You are here: Commands > Compare

Compare

Menu: Found in the DATA menu

The Compare command enables the comparison of a primary and a secondary tables to determine whether the two tables are the same and if not, to identify the differences between them. This command is useful for comparison of tables from two different points in time (for example: cheque registers, log files or master files).

The file comparison can be keyed or unkeyed. The most recent version of the data is considered the primary file, while the previous version of the data is considered the secondary file. The context of comparison results is based on this paradigm.

For all comparisons (unkeyed or keyed), the Compare command relies on consistency in the creation of the table’s content. To that end, for both files, field names referenced must be valid for both the primary and secondary file.

When the comparison is keyed, the Compare command also relies upon:

the existence of an ordered key field (such as for the Gaps command) containing unique values. Presort is not supported for the Compare command as the keyed comparison is based on the source files being provided ordered on the key field. If a sequence error is detected the comparison is halted.
the uniqueness of the key field. Any duplicate key values in either the primary or secondary file will be ignored with only the first instance being processed. The number of duplicates ignored in either file is reported in the Command Log.

Keyed vs. Unkeyed Comparisons

The benefit of a keyed comparison is that the results are more granular regarding adjacent changed records.

In an unkeyed comparison, adjacent inserted or deleted records could be mistaken as changes.

In a keyed comparison, adjacent changed records would be correctly interpreted.

Note: A key should not be contrived where one does not naturally exist. If the file contains a unique sorted key then you should specify it to get the most accurate results. If the file does not contain such a key then an unkeyed comparison should be used.

Parameters

The Compare command has the following command parameters: Append, If, First, Open, Next, To and While. For a description of these parameters, see Command Parameters. For a description of supported field modifiers see Field Modifiers.

Note: The IF, WHILE, FIRST and NEXT parameters should be used sparingly as they only exclude primary records from the comparison and as a result may cause apparent comparison differences that do not actually exist.

On

Specifies a key field for a keyed comparison. In the command dialog, check the “Unique sorted key” radio button and select the key field from the pull-down list or click [Choose] to display the selected fields dialog in which you can select fields and build expressions. For more information on the Selected Fields dialog, see Selection/Edit Dialog Boxes.

Fields

Specifies the field(s) to compare in an unkeyed comparison, and specifies additional fields to compare for each key value when a keyed comparison is performed. In the command dialog, select the field(s) from the “Fields to compare” list box or click [Choose] to display the selected fields dialog in which you can select fields and build expressions.

Maximum

Suppresses the detailed listing of values when there are more than the specified number of consecutive items. The default is 10 if not specified. This only applies for output to screen as all differences are included in data file output. In the command dialog, click the [More] button and enter the maximum in the “List fields for up to...” text box.

Errorlimit

Used to trigger an error message "Files are not the same" when n lines differ indicating that the two files are essentially dissimilar. If at any time there are n consecutive differences then the error message “Command cancelled” is generated in the Command Log followed by the message that the files are different. The default value is 5000 if not specified. This only applies for output to screen as all differences are included in data file output. In the command dialog, click the [More] button and enter the errorlimit in the “Stop processing after...” text box.

Tip: Whenever the secondary file is empty (i.e. contains no records) or whenever the specified Errorlimit is exceeded, the Compare command creates a WRITEn variable with a value of -1. This enables Compare command results to a data file within a procedure to be easily tested existence (no secondary file records) or for the compared files being significantly different (exceeding reasonable Errorlimit as currently set for the command).

Other

Enables fields aside from the fields being compared to be listed in the command results for records that are identified as having been inserted, deleted or changed. In the command dialog, click the [More] button, select the field(s) from the “Additional output fields” list box or click [Choose] to display the selected fields dialog in which you can select fields and build expressions.

The displayed value of OTHER fields will originate from:

Secondary file if a record is deemed to have been deleted
Primary file if record is deemed to have been inserted
Both Primary and Secondary files if record is deemed to have been changed

Specifying OTHER fields is useful where you are comparing field values between files for which another field has an expected one-to-one relationship that you want to confirm.

Command Mode Syntax

COMPARE <ON key-field><FIELDS field-list><OTHER field-list>

<MAXIMUM n> <ERRORLIMIT n>

<TO data-file-name|SCREEN> <IF test>

<WHILE test> <FIRST|NEXT range> <APPEND>¿

Note: The field names included in the field list must be valid for both the primary and secondary file.

Examples

The first example illustrates an un-keyed comparison of two monthly expense tables to identify the differences using the Compare command:

January Expenses

Record

February Expenses

Name

Balance

Number

Name

Balance

Alex

200.00

1

Anne

100.00

Anne

100.00

2

Bill

200.00

Bill

150.00

3

Fred

600.00

Cora

300.00

4

Greg

700.00

Dave

400.00

5

Jack

400.00

Eric

500.00

6

Cora

300.00

Mike

900.00

7

Hank

800.00

Lana

175.00

8

Eric

500.00

Performing an unkeyed comparison of the newer February Expenses table to the prior January Expenses table yields a table listing the Compare command results. The table contains a TYPE and a RECORD column along with the chosen fields.

Type

Record Number

Name

Balance

D

1

Alex

200.00

F

T

2

2

Bill

Bill

150.00

200.00

A

3

Fred

600.00

A

4

Greg

700.00

A

5

Jack

400.00

F

T

7

7

Dave

Hank

400.00

800.00

D

9

Mike

900.00

D

9

Lana

175.00

The TYPE column indicates whether the a record has been added, deleted of changed (from the perspective of the primary table):

A indicates a record that has been added
D indicates a record that has been deleted
F indicates that the record has been changed from the specified value
T indicates that the record has been changed to the specified value

The RECORD NUMBER column indicates which relative record has been added, deleted or changed (from the perspective of the primary table).

The second example illustrates a keyed comparison of two monthly expense tables already ordered (sorted) on a key field (Name). The key field is used to identify the differences using the Compare command.

January Expenses

Record

February Expenses

Name

Balance

Number

Name

Balance

Alex

200.00

1

Anne

100.00

Anne

100.00

2

Bill

200.00

Bill

150.00

3

Cora

200.00

Cora

300.00

4

Eric

500.00

Dave

400.00

5

Fred

600.00

Eric

500.00

6

Greg

700.00

Lana

175.00

7

Hank

800.00

Mike

900.00

8

Jack

400.00

The sorted key field for the comparison is Name and the additional compare field is Balance.

Performing a keyed comparison of the newer sorted February Expenses table to the prior sorted January Expenses table yields a table listing the Compare command results. The table contains a TYPE and a RECORD column along with the chosen fields (Name and Balance).

Type

Record Number

Name

Balance

D

1

Alex

200.00

F

T

2

2

Bill

Bill

150.00

200.00

F

T

3

3

Cora

Cora

300.00

200.00

D

4

Dave

400.00

A

5

Fred

600.00

A

6

Greg

700.00

A

7

Hank

800.00

A

8

Jack

400.00

D

9

Lana

175.00

D

9

Mike

900.00

Note: Whenever a keyed comparison is performed, the key field is expected to contain unique key values. If duplicate key values are encountered, the duplicate values are ignored and only the first unique key value is processed. The Compare command will report the number of duplicate key values skipped when performing a keyed comparison in the Command Log.