Compare two files on mainframe – DFSORT.

Compare two files on mainframe and write the match & no-match records

 

If you work in a Mainframe shop – once in a while you’ll get to the situation where you’ll want to compare two files* and your mind might wander to answer the question in the subject line above heading real quick. You may compare two files on mainframe using DFSORT or possibly SyncSort, however, we’ll talk about DFSORT in this article.

This can be done using a utility (SORT) instead of writing a COBOL program. Here, you might also be tempted to ask if writing a program is better or writing a SORT code is – well, I’d say we’ll leave that discussion for other time and for now will discuss about how can we do it using the SORT at your shop.

Your shop can have DFSORT or SyncSort, usually not both of them. After all they are pay-for-product and you would not want to pay twice for the same vehicle. DFSORT and SyncSort are the predominant Mainframe sorting products. Their control cards have many similarities, and some differences. We’ll discuss both of them below.

For the below example some consideration are as follows:

  1. Files are fixed-block, RECFM=FB
  2. Files length is 80, LRECL=80
  3. The key on which the files are compared starts at position and are 7 characters long.

Let’s directly get to the work – the following SORT statements should get you what you want:

JOINKEYS FILE=F1,FIELDS=(key1startpos,7,A)
JOINKEYS FILE=F2,FIELDS=(key2startpos,7,A)
JOIN UNPAIRED,F1,F2
REFORMAT FIELDS=(F1:1,80,F2:1,80)
SORT FIELDS=COPY

A “JOINKEYS” is made up of three Tasks. Sub-Task-1 –  is the first JOINKEYS. Sub-Task-2 is the second JOINKEYS. Sub-Task-3, the Main Task, follows and is where the joined data is processed. In the example above it is a simple COPY operation. The joined data will simply be written to SORTOUT.

The JOIN statement defines that as well as matched records, UNPAIRED F1 and F2 records are to be presented to the Main Task. The REFORMAT statement defines the record which will be presented to the Main Task.

We need the matched and non-matched records in different files.  For this in  DFSORT, there is a matching-marker, specified with ? in the REFORMAT, as shown below:

 REFORMAT FIELDS=(F1:1,80,F2:1,80,?)

This increases the length of the REFORMAT record by one byte. The ? is optional and can be specified anywhere on the REFORMAT record, and need not be specified. The ? is resolved by DFSORT to: B, data sourced from Both files; 1, unmatched record from F1; 2, unmatched record from F2.

The requirement is for all unmatched records, from either F1 or F2, to be written to one file. This requires a REFORMAT statement which includes both records completely. For DFSORT and SYNCSORT the statements will be different. For SyncSort we’ll talk later.

DFSORT, output unmatched records:

 REFORMAT FIELDS=(F1:1,80,F2:1,80,?)
  OUTFIL FNAMES=NOMATCH,INCLUDE=(161,1,SS,EQ,C'1,2'),
        IFTHEN=(WHEN=(161,1,CH,EQ,C'1'),
                    BUILD=(1,80)),
        IFTHEN=(WHEN=NONE,
                    BUILD=(81,80))

Getting matched records out are easy:

  OUTFIL FNAMES=MATCH,SAVE

Putting all pieces together will look like this:

DFSORT:

JOINKEYS FILE=F1,FIELDS=(1,7,A)
  JOINKEYS FILE=F2,FIELDS=(1,7,A)
  JOIN UNPAIRED,F1,F2
  REFORMAT FIELDS=(F1:1,80,F2:1,80,?)
  SORT FIELDS=COPY
  OUTFIL FNAMES=NOMATCH,INCLUDE=(161,1,SS,EQ,C'1,2'),
        IFTHEN=(WHEN=(161,1,CH,EQ,C'1'),
                    BUILD=(1,80)),
        IFTHEN=(WHEN=NONE,
                    BUILD=(81,80))
  OUTFIL FNAMES=MATCH,SAVE,
     BUILD=(1,80)

 

* – May be you want to compare more files – but we’ll pick two files for current post. For more files we might talk later.