| Syntax Color Legend |
|---|
| Command |
| Mandatory parameter |
| Optional parameter |
| Mandatory keyword |
| Optional keyword |
COMPARE (<File 1>, <Layout 1>[, <Start line number id 1>] [, SKIPCLEAR], <File 2> , <Layout 2>[, <Start line number id 2>] [, SKIPCLEAR]) [RESULTOKTO <Status>]
<Keylist>
<Code lines>
ENDCOMPARE
Creates a loop that compares two files record by record, indicating in SI-FILEID whether a match is found or not. During the comparison process, the system item SI-FILEID is filled with spaces if the records in the file IDs match, the file ID with the lower value if the key is set to ascending or the file ID with the higher value if the key is set to descending. In the following loop cycle, the next record of the file ID in SI-FILEID is read. If it is empty, the next record of both files is read.
None
| Parameter | Description | |
|---|---|---|
| <File 1> | File id for the file that must be compared with <File 2>. | |
| <Layout 1> | Layout definition that is used for organizing <File 1>. | |
| <Start line number id 1> | When set, <File 1> is read from this line on. | |
| SKIPCLEAR | With this option, an existing CLEARATFIRSTREAD option set in the NAMEFILE command will not be executed (i.e. the existing file will not be cleared when the COMPARE is the first action on the file after the NAMEFILE command). This option can be set for both files individually. | |
| <File 2> | File id for the file that must be compared with <File 1>. | |
| <Layout 2> | Layout definition that is used for organizing <File 2>. | |
| <Start line number id 2> | When set, <File 2> is read from this line on. | |
| <Status> | Variable that holds the result status. This variable can be defined as a boolean or as an alphanumeric variable. In case execution of COMPARE...ENDCOMPARE fails (files don't exist), the result status is set to "False" (for boolean) or "*****" (for alphanumeric). | |
| <Keylist> | List of keys, denoting the fields to compare. Each key has the following syntax:
<New line> is a separate line for each added key <Field 1> is an expression giving the variable for the first file and <Field 2> is an expression giving the variable for the second file. |
|
| ASC | Compares the key IDs in ascending order. ASC: fileA.key1 < fileB.key1 results in SI-FILEID = fileA. | |
| DSC | Compares the key IDs in descending order. DSC: fileA.key1 > fileB.key1 results in SI-FILEID = fileB. | |
| If there is a match SI-FILEID will be spaces. | ||
| <Code lines> | Code lines that are executed within the COMPARE loop. | |
| RESULTOKTO | Command that directs the result status of a command to the specified boolean or alphanumeric (length 5) variable (instead of using the system item RESOK). This command must always be specified on the same line as the COMPARE instruction itself. | |
In the example shown below two files are compared, an English and a Dutch file with addresses.
The files are compared with the key's streetname and housenumber on which the files were sorted as well. When the address is the same in both files, the record is written to a "match" file. Otherwise, the address not present in the other file is written to a "unique" file.
namefile (f_ad_nl, 'adres.txt')
namefile (f_ad_en, 'address.txt')
namefile (f_addressmatch, 'addressmatch.txt')
namefile (f_addressunique, 'addressunique.txt')
compare (f_ad_nl, r_adres, f_ad_en, r_address)
key (r_adres.straat, r_address.street, asc)
key (r_adres.huisnummer, r_address.number, asc)
if si-fileid = ''
// The addresses match
writefile (f_addressmatch, r_adres)
elseif si-fileid = 'F_AD_NL'
// The addresses did not match, the address in the Dutch file is a smaller value
writefile (f_addressunique, r_adres)
elseif si-fileid = 'F_AD_EN'
// The addresses did not match, the address in the English file is a smaller value
writefile (f_addressunique, r_address)
endif
endcompare