Cobol shortcut using sort
Posted on December 21, 2009
One of the more common tasks for a Cobol program is to take some action on matching records from two flat files. This is fairly straightforward, but it’s still a pain, because you have to be meticulous about testing a situation like what happens when one file hits EOF before the other one.
Usually, one just copies the code from a similar program, but there are still many details to take care of. The join parameter of sort, however, can make this process much easier. Let sort (either Syncsort or DFSort–they both work the same) take care of all the messy details, and you just code what happens after all the matching has taken place.
Let’s take an example: you have file A and file B, and you want to replace a field in A with a field from B only if the records match.
- Create a sort step in front of your Cobol program. Code the sort parm like this:
SORT FIELDS=COPY �
JOINKEYS FILES=F1,FIELDS=(358,10,A)
JOINKEYS FILES=F2,FIELDS=(145,10,A)
REFORMAT FIELDS(F1:1,600, �
F2:1,200),FILL=C’X’
F1 is file A, with a DD card in the sort of SORTJNF1, and file B is F2, with a DD card as SORTJNF2. The FIELDS parm defines the field to match on: position 358, for 10 characters in file A, and position 145 for file B.
The reformat parm defines the output record: the 600 bytes from file A, and 200 from file B. It can be part of the record or the whole records, just as in an outrec or inrec parm.
Note the fill character–X. I use it here, but you can also use something “safer,” like high values, if you’re worried that multiple X’s could be confused with valid data.
Now, the output record will consist of an 800 byte record, made up of record A joined with record B, and will only be done for those records that have matching fields from file A and file B.
So, the hard part is done. You don’t need to worry about any of that messy logic in your cobol program. Define the input file in your Cobol program like this:
01 IN-A-B.
03 RECA PIC X(600).
03 RECB PIC X(200).
And you’re done. There are more complicated conditions, where you also want to work with unmatched conditions, where you want to process all records from file A that are not matched on file B. In that case, you use an extract parm card in the sort:
This will still give you joined records of 800 bytes, but the unmatched record will be all X’s. So, you can easily check for that condition in your Cobol program. If RECA = all ‘X”, then you have an unmatched condition for RECB. And vice-versa.
And, you can use this same sort processing to match up 3 files. I’m sure you can see the possibilities. I always use this technique now when I have to match up two files. Try it, and I think you’ll agree that it will make your coding not only faster, but easier to test.
» Filed Under Mainframe
Comments
2 Responses to “Cobol shortcut using sort”
Leave a Reply
Facebook
Google Profile
LinkedIn
Tripit
Twitter profile
Business RSS feed
Family RSS feed
Mainframe RSS feed
Vacation RSS feed
Looks interesting, however I cannot find any documentation in the IBM DFSORT manuals. Is this a DFSORT technique or Syncsort or ? Can you give us an IBM (or other) Manual reference? Thank you for sharing.
It’s available with both DFSORT and Syncsort. Just google the words dfsort and join, or with syncsort, and you’ll see plenty of references.