z/OS DFSORT: Getting Started
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Reformatting records with IFTHEN

z/OS DFSORT: Getting Started
SC23-6880-00

The OVERLAY, FINDREP, and BUILD or FIELDS parameters discussed in previous sections let you use the same reformatting items for every output record. IFTHEN clauses for the OUTREC statement let you select subsets of the output records and apply different BUILD, FINDREP or OVERLAY items to them. So, for example, you can apply a set of BUILD items to "type 1" records, a set of OVERLAY items to "type 2" records, do find and replace operations on "type 3" records, and apply a different set of OVERLAY items (or no items) to other types of records.

You can use five types of IFTHEN clauses as follows:
  • WHEN=INIT: Use one or more WHEN=INIT clauses to apply BUILD, FINDREP or OVERLAY items to all of your input records. WHEN=INIT clauses and WHEN=GROUP clauses are processed before any of the other IFTHEN clauses.
  • WHEN=GROUP: Use one or more WHEN=GROUP clauses to propagate fields, identifiers and sequence numbers within groups of records. You define the records that belong to a group using an appropriate combination of BEGIN=(logexp), END=(logexp), KEYBEGIN=(field) and RECORDS=n parameters.

    You can use any logical expression for BEGIN=(logexp) and END=(logexp) that you can use for the COND=(logexp) parameter of an INCLUDE statement as previously discussed in Including or omitting records. A BEGIN=(logexp) or END=(logexp) parameter is satisfied when the logical expression evaluates as true.

    A KEYBEGIN=(field) parameter is satisfied when the field value changes.

    You define how the records in a group are to be changed using the PUSH parameter. You use c: (output column), p,m (field), ID=n (zoned decimal identifier of length n) and SEQ=n (zoned decimal sequence number of length n) in the PUSH parameter to specify how the records in the group are to be changed.

    WHEN=INIT clauses and WHEN=GROUP clauses are processed before any of the other IFTHEN clauses.

  • WHEN=(logexp): Use one or more WHEN=(logexp) clauses to apply BUILD, FINDREP or OVERLAY items to the subset of your records that satisfy a specified logical expression. You can use any logical expression for WHEN=(logexp) that you can use for the COND=(logexp) parameter of an INCLUDE statement as previously discussed in Including or omitting records. A WHEN=(logexp) clause is satisfied when the logical expression evaluates as true.
  • WHEN=ANY: Use a WHEN=ANY clause after multiple WHEN=(logexp) clauses to apply additional BUILD, FINDREP or OVERLAY items to your records if they satisfied the criteria for any of the preceding WHEN=(logexp) clauses.
  • WHEN=NONE: Use one or more WHEN=NONE clauses to apply BUILD, FINDREP or OVERLAY items to your records that did not meet the criteria for any of the WHEN=(logexp) clauses. WHEN=NONE clauses are processed after any of the other IFTHEN clauses. If you do not specify a WHEN=NONE clause, only the WHEN=INIT changes (if any) and WHEN=GROUP changes (if any) are applied to input records that do not meet the criteria for any of the WHEN=(logexp) clauses.
Suppose you want to produce a report showing the title, publisher, edited price and % discount if you:
  • discount the price of books from publisher COR that cost more than $20.00 by 20%
  • discount the price of books from publisher COR that cost $20.00 or less by 10%
  • discount the price of all books from publisher VALD by 25%
  • discount the price of all books from the other publishers by 15%
You can use the following statements:
  OPTION COPY
  OUTREC IFTHEN=(WHEN=INIT,
      BUILD=(1:1,40,50:106,4,60:170,4,BI,EDIT=(TT.TT),70:170,4)),
    IFTHEN=(WHEN=(50,4,CH,EQ,C'COR',AND,70,4,BI,GT,+2000),
      OVERLAY=(70:C'20%',X)),
    IFTHEN=(WHEN=(50,4,CH,EQ,C'COR',AND,70,4,BI,LE,+2000),
      OVERLAY=(70:C'10%',X)),
    IFTHEN=(WHEN=(50,4,CH,EQ,C'VALD'),OVERLAY=(70:C'25%',X)),
    IFTHEN=(WHEN=NONE,OVERLAY=(70:C'15%',X))

Table 1 shows the copied data set.

Table 1. Proposed discounts for books
Book Title Publisher Price Discount

1   40

50  53

60  64

70  73

COMPUTER LANGUAGES
LIVING WELL ON A SMALL BUDGET
SUPPLYING THE DEMAND
VIDEO GAME DESIGN
INKLINGS: AN ANTHOLOGY OF YOUNG POETS
COMPUTERS: AN INTRODUCTION
PICK'S POCKET DICTIONARY
EDITING SOFTWARE MANUALS
NUMBERING SYSTEMS
STRATEGIC MARKETING
THE INDUSTRIAL REVOLUTION
MODERN ANTHOLOGY OF WOMEN POETS
INTRODUCTION TO PSYCHOLOGY
THE COMPLETE PROOFREADER
SYSTEM PROGRAMMING
SHORT STORIES AND TALL TALES
INTRODUCTION TO BIOLOGY
ADVANCED TOPICS IN PSYCHOANALYSIS
EIGHTEENTH CENTURY EUROPE
CRISES OF THE MIDDLE AGES

FERN
COR
COR
VALD
COR
WETH
COR
VALD
FERN
VALD
WETH
COR
COR
FERN
WETH
VALD
VALD
FERN
WETH
COR

26.00
99.00
19.25
21.99
05.95
18.99
02.95
14.50
03.60
23.50
07.95
04.50
22.00
06.25
31.95
15.20
23.50
26.00
17.90
12.00

15%
20%
10%
25%
10%
15%
10%
25%
15%
25%
15%
10%
20%
15%
15%
25%
25%
15%
15%
10%

The first IFTHEN clause is a WHEN=INIT clause that uses BUILD to initialize every record with the title, publisher, edited price and a placeholder for the discount. Notice that the publisher field is copied to positions 50-53 and the 4-byte BI price field is copied to positions 70-73. Subsequent IFTHEN clauses are processed for all records and must refer to these fields at their new positions.

The second IFTHEN clause is a WHEN=(logexp) clause that uses OVERLAY to set the discount field to '20%' for the subset of records with 'COR ' as the publisher and more than 2000 as the price. Subsequent IFTHEN clauses are not processed for records that satisfy this logical expression. Subsequent IFTHEN clauses are processed for records that do not satisfy this logical expression.

The third IFTHEN clause is a WHEN=(logexp) clause that uses OVERLAY to set the discount field to '10%' for the subset of records with 'COR ' as the publisher and 2000 or less as the price. Subsequent IFTHEN clauses are not processed for records that satisfy this logical expression. Subsequent IFTHEN clauses are processed for records that do not satisfy this logical expression.

The fourth IFTHEN clause is a WHEN=(logexp) clause that uses OVERLAY to set the discount field to '25%' for the subset of records with 'VALD' as the publisher. Subsequent IFTHEN clauses are not processed for records that satisfy this logical expression. Subsequent IFTHEN clauses are processed for records that do not satisfy this logical expression.

The fifth IFTHEN clause is a WHEN=NONE clause that uses OVERLAY to set the discount field to '15%' for the subset of records that did not satisfy the logical expression for the second, third and fourth IFTHEN clauses.

DFSORT determines an appropriate reformatted output record length from the IFTHEN clauses you specify. However, you can use the IFOUTLEN=n parameter to tell DFSORT the length you want it to use for the reformatted records. For example, suppose you had 80 byte input records, and this DFSORT statement:
   OUTREC IFTHEN=(WHEN=INIT,OVERLAY=(81:SEQNUM,8,ZD)),
                    IFTHEN=(WHEN=(81,8,ZD,EQ,+5),OVERLAY=(21:C'J82')),
                    IFTHEN=(WHEN=(81,8,ZD,EQ,+10),OVERLAY=(21:C'M72'))
By default, DFSORT would set the reformatted output record length to 88 to accommodate the 8-byte ZD sequence number you added. If you're only using the 8-byte sequence number for the WHEN comparisons, and you don't want the sequence number as part of the reformatted output record, you could use this statement instead to tell DFSORT to use 80 for the reformatted output record length instead of 88:
   OUTREC IFTHEN=(WHEN=INIT,OVERLAY=(81:SEQNUM,8,ZD)),
                    IFTHEN=(WHEN=(81,8,ZD,EQ,+5),OVERLAY=(21:C'J82')),
                    IFTHEN=(WHEN=(81,8,ZD,EQ,+10),OVERLAY=(21:C'M72')),
          IFOUTLEN=80
Suppose you have the following 30-byte FB input records:
C33  Not in a group
HDR   Start Group 1
A01    Group 1 record
B02    Group 1 record
C03    Group 1 record
TRL   End Group 1
R24  Not in a group
T02  Not in a group
HDR   Start Group 2
D04    Group 2 record
E05    Group 2 record
TRL   End Group 2
F97  Not in a group
In the output data set we only want to include groups of records that start with 'HDR' and end with 'TRL'. We write the following DFSORT control statements:
  OPTION COPY
  OUTREC IFTHEN=(WHEN=GROUP,BEGIN=(1,3,CH,EQ,C'HDR'),
    END=(1,3,CH,EQ,C'TRL'),PUSH=(31:ID=1))
  OUTFIL INCLUDE=(31,1,CH,NE,C' '),BUILD=(1,30)
We use an IFTHEN WHEN=GROUP clause to put a non-blank character in each record that is part of a group. BEGIN indicates a group starts with a record that has 'HDR' in positions 1-3. END indicates a group ends with a record that has 'TRL' in positions 1-3. PUSH overlays a 1-byte ID character at position 31 in each record of a group (after the end of the record). After the IFTHEN GROUP clause is executed, the intermediate records look like this:
C33  Not in a group
HDR   Start Group 1           1
A01    Group 1 record         1
B02    Group 1 record         1
C03    Group 1 record         1
TRL   End Group 1             1
R24  Not in a group
T02  Not in a group
HDR   Start Group 2           2
D04    Group 2 record         2
E05    Group 2 record         2
TRL   End Group 2             2
F97  Not in a group

Note that the records within a group have a non-blank character in position 31 whereas the records outside groups have a blank character in position 31. The ID starts at 1 for the first group and is incremented by 1 for each subsequent group. Since we are only allowing one character for the ID, when the ID counter gets to 10, a '0' will appear in position 31. That's fine since we are just looking for a non-blank to indicate a record within a group, or a blank to indicate a record outside of a group.

We use an OUTFIL statement to only INCLUDE records with a non-blank in position 31, and to remove the ID character so the included output records will be identical to the input records. After the OUTFIL statement is executed, the final output records look like this:
HDR   Start Group 1
A01    Group 1 record
B02    Group 1 record
C03    Group 1 record
TRL   End Group 1
HDR   Start Group 2
D04    Group 2 record
E05    Group 2 record
TRL   End Group 2

For detailed information on using IFTHEN clauses and IFOUTLEN, see z/OS DFSORT Application Programming Guide.

So far
Now you know how to use IFTHEN clauses with the OUTREC statement to reformat different records in different ways and to perform group operations. Keep in mind that you can use IFTHEN clauses with the INREC statement and OUTFIL statement, as well as with the OUTREC statement. Next, you will learn about some special considerations for using the INREC statement.

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014