Operating Systems

DFSORT: IFTHEN, Numeric Tests & Relative Dates

4 Pages

One of the most useful features of programming languages is their ability to do conditional logic, which can be described conceptually as IF something, THEN something. Although IBM’s DFSORT isn’t a programming language, it has its own flavor of conditional logic for reformatting records. Appropriately called IFTHEN clauses, this feature can be used in a wide variety of situations and often makes it possible to replace multiple passes over the data with a single pass over the data. This article examines three examples that illustrate some things you can do with IFTHEN clauses and other new DFSORT features. Future articles will discuss additional features for manipulating data in various ways, including how to parse variable fields such as comma-separated values and delimited fields into fixed fields.

Let’s start with a simple, but useful example relating to multiple record layouts in the same input data set. Figure 1 shows an input file with a header record identified with ‘hd’ in positions one through two, detail records with no identifiers, and a trailer record with ‘trl’ in positions one through three. The three types of records (header, detail, and trailer) also have different record layouts. The header record has a date in positions 21 through 28 that we want to overlay with the date of the run. We want to sort on the bird names, but keep the header as the first record and the trailer as the last record. Just sorting on the bird names in positions 15 through 26 won’t work because the header and trailer records have different values in those positions; the output would be as shown in Figure 2. We certainly don’t want the trailer and header in the middle of the detail records.

Figure 1 shows the DFSORT control statements we need to sort the records correctly and to update the date in the header record. The new function here is the IFTHEN clause and its WHEN, OVERLAY and BUILD subparameters. We’ll discuss what each control statement does as well as the new IFTHEN function. This particular example uses the IFTHEN function while sorting, but you also can use it while copying or merging.

We use three IFTHEN clauses in the INREC statement to handle the three different record types in three different ways:

  • The first IFTHEN clause uses WHEN=INIT to indicate that it applies to all the records; OVERLAY puts a temporary ‘B’ indicator in position 81 of every record.
  • The second IFTHEN clause uses WHEN=(1,2,CH,EQ,C’hd’) to indicate that it applies only to records with ‘hd’ in positions one through two; OVERLAY puts a temporary ‘A’ indicator in position 81 of the ‘hd’ record, and also uses DATE3(-) to replace the old ‘yyyy-ddd’ date at positions 21 through 28 with the current ‘yyyyddd’ date for the run.
  • The third IFTHEN clause uses WHEN=(1,3,CH,EQ,C’trl’) to indicate that it applies only to records with ‘trl’ in positions one through three; OVERLAY puts a temporary ‘C’ indicator in position 81 of the ‘trl’ record.

As a result, we have ‘A’ in 81 for the header record, ‘B’ in 81 for all the detail records, and ‘C’ in 81 for the trailer record.

We use the SORT statement, which is processed after the INREC statement, to sort on our new indicator in position 81, and then on the bird names in positions 15 through 26. The ‘A’ record (header) is sorted before the ‘B’ (detail) records, and the ‘C’ record (trailer) is sorted after the ‘B’ (detail) records. Since the first sort field is ‘B’ for all the detail records, they’re further sorted on the second sort field (bird name). The header record is kept as the first record and the trailer record is kept as the last record, with the detail records sorted in between by the bird name.

Finally, we use the OUTREC statement to remove the temporary indicator from position 81. Figure 1 shows the correctly sorted output records.

IFTHEN Clauses

You can use the new IFTHEN function on the INREC, OUTREC, and OUTFIL statements to conditionally reformat records. For each IFTHEN clause, you must specify a WHEN parameter to identify the records the clause applies to, and an OVERLAY or BUILD parameter to indicate the actions to be taken for those records.

4 Pages