z/OS DFSORT Application Programming Guide
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Example 26

z/OS DFSORT Application Programming Guide
SC23-6878-00

  INREC IFTHEN=(WHEN=INIT,
     PARSE=(%1=(FIXLEN=10,STARTAFT=C'First="',ENDBEFR=C'"'))),
    IFTHEN=(WHEN=INIT,
     PARSE=(%2=(FIXLEN=10,STARTAFT=C'Middle="',ENDBEFR=C'"'))),
    IFTHEN=(WHEN=INIT,
     PARSE=(%3=(FIXLEN=10,STARTAFT=C'Last="',ENDBEFR=C'"'))),
    IFTHEN=(WHEN=INIT,
     PARSE=(%4=(FIXLEN=10,STARTAFT=C'Wife="',ENDBEFR=C'"')),
     BUILD=(%1,13:%2,25:%3,37:%4))
  SORT FIELDS=(25,10,CH,A,1,10,CH,A,13,10,CH,A)
  OUTFIL HEADER2=('First',13:'Middle',
      25:C'Last',37:'Wife',/,10C'-',13:10C'-',
      25:10C'-',37:10C'-')  

This example illustrates how you can create a sorted report from FB input records with keyword values that can occur in any order or not occur at all.

The 80-byte input records might look like this:
   Last="Buchanan" First="James"
  Wife="Louisa" First="John" Middle="Quincy" Last="Adams"
   First="George" Last="Washington" Wife="Martha"
 Last="Clinton" Wife="Hillary" Middle="Jefferson" First="William"
  First="John" Wife="Abigail" Last="Adams"

Note that each record has up to four variable fields each identified by a specific keyword (First, Middle, Last, Wife). In each record, the fields can be in any order, do not start and end in the same position and can have different lengths.

We want to sort the records by last name, first name and middle name, and create a report with fixed headings and values for the first name, middle name, last name and wife's name that looks like this:
First       Middle      Last        Wife
----------  ----------  ----------  ----------
John                    Adams       Abigail
John        Quincy      Adams       Louisa
James                   Buchanan
William     Jefferson   Clinton     Hillary
George                  Washington  Martha

In order to extract and sort variable fields like these, we use a separate IFTHEN PARSE clause for each keyword. This allows us to find a particular keyword anywhere in the record or ignore it if it is not in the record. If we used PARSE instead of IFTHEN PARSE, a missing keyword would cause any keywords that followed to be ignored. But because each IFTHEN PARSE starts scanning at position 1 by default, we can look for each keyword independently of the others. If a keyword is missing, the parsed field is set to all blanks (for example, %2 and %4 are set to blanks for the James Buchanan record).

We use %1 to create a 10-byte fixed parsed field into which we extract the first name. We use %2 to create a 10-byte fixed parsed field into which we extract the middle name. We use %3 to create a 10-byte fixed parsed field into which we extract the last name. We use %4 to create a 10-byte fixed parsed field into which we extract the wife's name. We use BUILD to reformat the records to contain the fixed parsed fields. Then we SORT on the fixed parsed fields. Finally, we use OUTFIL to create the headings.

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014