IFTHEN

IFTHEN statements allow you to reformat different records in different ways by specifying how build or overlay items are to be applied to records that meet given criteria. Use IFTHEN statements if you want to insert, rearrange, delete or overlay fields in different ways for different records. IFTHEN clauses for the OUTREC statement can be used to select subsets of the output records and apply different BUILD, FINDREP or OVERLAY items to them.

You can use four types of IFTHEN statements as follows:

SYNTAX WHEN=INIT

Use one or more WHEN=INIT clauses  to apply build or overlay items to all of your input records. WHEN=INIT clauses are processed before any of the other IFTHEN clauses.

INREC IFTHEN=(WHEN=INIT, BUILD=(1,20,21:C’Department’,31:3X,21,60)), 
      IFTHEN=(WHEN=(5,2,CH,EQ,C’D1’),OVERLAY=(31:8,3)), 
      IFTHEN=(WHEN=(5,2,CH,EQ,C’D2’),OVERLAY=(31:12,3)) 

SYNTAX WHEN=(logexp)

Use one or more WHEN=(logexp) clauses to apply build or overlay items to your input records that meet specified criteria. A WHEN=(logexp) clause is satisfied when the logical expression evaluates as true.

INREC IFTHEN=(WHEN=(1,3,CH,EQ,C’T01’,AND,18,4,ZD,LE,+2000),
      OVERLAY=(42:C’Type1 <= 2000’),HIT=NEXT),
      IFTHEN=(WHEN=(1,3,CH,EQ,C’T01’,AND,6,1,BI,BO,X’03’),
      BUILD=(1,21,42,13)),
      IFTHEN=(WHEN=(1,3,CH,EQ,C’T01’,AND,18,4,ZD,GT,+2000),
      OVERLAY=(42:C’Type1 > 2000 ’),HIT=NEXT),
      IFTHEN=(WHEN=(1,3,CH,EQ,C’T01’,AND,6,1,BI,BO,X’01’),
      BUILD=(1,25,42,13))

SYNTAX WHEN=ANY

Use a WHEN=ANY clause after multiple WHEN=(logexp) clauses to apply additional build or overlay items to your input records if they satisfied the criteria for any of the preceding WHEN=(logexp) clauses.

INREC IFTHEN=(WHEN=(1,3,SS,EQ,C’T01,T02,T03’),
      BUILD=(C’Group A’,X,1,80),HIT=NEXT),
      IFTHEN=(WHEN=(1,3,SS,EQ,C’T04,T05,T06’),
      BUILD=(C’Group B’,X,1,80),HIT=NEXT),
      IFTHEN=(WHEN=(1,3,SS,EQ,C’T07,T08,T09,T10’),
      BUILD=(C’Group C’,X,1,80),HIT=NEXT),
      IFTHEN=(WHEN=ANY,OVERLAY=(16:C’Group Found’)

SYNTAX WHEN=NONE

Use one or more WHEN=NONE clauses to apply build or overlay items to your input 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) are applied to input records that do not meet the criteria for any of the WHEN=(logexp) clauses. 

INREC IFTHEN=(WHEN=INIT,BUILD=(1,20,21:C’Department’,31:3X,21,60)),
      IFTHEN=(WHEN=(5,2,CH,EQ,C’D1’),OVERLAY=(31:8,3)),
      IFTHEN=(WHEN=(5,2,CH,EQ,C’D2’),OVERLAY=(31:12,3)),
      IFTHEN=(WHEN=NONE,OVERLAY=(31:C’***’))

IFTHEN clauses are processed in the following order:

  • WHEN=INIT clauses
  • WHEN=(logexp) clauses and WHEN=ANY clauses
  • WHEN=NONE clauses

Example: IFTHEN processing order

INREC IFTHEN=(WHEN=(12,1,BI,ALL,X’3F’),OVERLAY=(18:C’Yes’)),
      IFTHEN=(WHEN=(35,2,PD,EQ,+14),BUILD=(1,40,45,3,HEX),HIT=NEXT),
      IFTHEN=(WHEN=(35,2,PD,GT,+17),BUILD=(1,40,41,5,HEX),HIT=NEXT),
      IFTHEN=(WHEN=ANY,BUILD=(1,55,C’ABC’,70:X)),
      IFTHEN=(WHEN=(63,2,CH,EQ,C’AB’),OVERLAY=(18:C’No’)),
      IFTHEN=(WHEN=NONE,BUILD=(1,40,51,8,TRAN=LTOU)) 

For this example, the IFTHEN clauses are processed as follows:

  • If clause 1 is satisfied, its overlay item is applied and processing stops.
  • If clause 1 is not satisfied, its overlay item is not applied and processing continues.
  • If clause 2 is satisfied, its build items are applied and processing continues.
  • If clause 2 is not satisfied, its build items are not applied and processing continues.
  • If clause 3 is satisfied, its build items are applied and processing continues.
  • If clause 3 is not satisfied, its build items are not applied and processing continues.
  • If clause 4 is satisfied, its build items are applied and processing stops.
  • If clause 4 is not satisfied, its build items are not applied and processing continues.
  • If clause 5 is satisfied, its overlay item is applied and processing stops.
  • If clause 5 is not satisfied, its overlay item is not applied and processing continues.
  • If clause 6 is satisfied, its build items are applied and processing stops.
  • If clause 6 is not satisfied, its build items are not applied and processing stops.

PARSE fields using IFTHEN parameter : Click Here

Example: IFTHEN Header and Trailer with data formatting

//STEP01  EXEC PGM=SORT   
//SYSOUT  DD SYSOUT=*  
//SORTIN  DD DSN=INPUT.FILE1,DISP=SHR 
//        DD DSN=INPUT.FILE2,DISP=SHR  
//        DD DSN=INPUT.FILE3,DISP=SHR 
//SORTOUT DD DSN=OUTPUT.FILE,DISP=(NEW,CATLG,DELETE),
//           SPACE=(CYL(5,5)),UNIT=SYSDA 
//SYSIN   DD *  
  INREC IFTHEN=(WHEN=(1,3,CH,EQ,C'HDR'),
        OVERLAY=(6:YDDD=(D4/),81:C'0',82:SEQNUM,2,ZD)),
        IFTHEN=(WHEN=(1,3,CH,EQ,C'TRL'),
        OVERLAY=(11:YDDD=(D4/),81:C'9',82:SEQNUM,2,ZD)),
        IFTHEN=(WHEN=NONE,
        OVERLAY=(81:C'1'))
  SORT FIELDS=(81,1,CH,A,8,5,CH,A)
  OUTFIL REMOVECC,
  OMIT=(81,1,SS,EQ,C'0,9',AND,82,2,ZD,GT,+1),
  OUTREC=(1,80) 
//* 

This example shows how you can use three input files, each with a header record (‘HDR’), detail records (‘DTL’) and a trailer record (‘TRL’), and create an output file with one header record with the current date, the sorted detail records, and one trailer record with the current date.

INREC statement. The first IFTHEN WHEN=(logexp) clause identifies and operates on header records (‘HDR’ in positions 1-3); OVERLAY puts today’s date in the form ‘ddd/yyyy’ in positions 6-13, adds a ‘0’ in position 81, adds a ZD sequence number in positions 82-83 and does not affect the rest of the record.

The second IFTHEN WHEN=(logexp) clause identifies and operates on trailer records (‘TRL’ in positions 1-3); OVERLAY puts today’s date in the form ‘ddd/yyyy’ in positions 11-18, adds a ‘9’ in position 81, adds a ZD sequence number in positions 82-83 and does not affect the rest of the record.

The IFTHEN WHEN=NONE clause identifies and operates on detail records (not ‘HDR’ or ‘TRL’ in positions 1-3); OVERLAY adds a ‘1’ in position 81 and does not affect the rest of the record.

DFSORT extends the reformatted input records from 80 bytes to 83 bytes to accommodate the identifier byte added in position 81 and the sequence number added in positions 82-83.

The ‘0’, ‘1’ or ‘9’ identifier byte added in position 81 allows us to sort the header records (‘0’) first, followed by the detail records (‘1’), and then the trailer records (‘9’). The sequence number added in positions 82-83 will allow us to keep only the first header record and the first trailer record. The sequence number will be 1 for the first header record, 2 for the second header record and 3 for the third header record. Likewise, the sequence number will be 1 for the first trailer record, 2 for the second trailer record and 3 for the third trailer record. Since the sequence number is not specified for the detail records, it will be blank.

SORT statement. FIELDS specifies an ascending 1-byte character control field at position 81 (the identifier byte added by INREC), and an ascending 5-byte character control field starting at position 8 (the key for the detail records).

REMOVECC removes the ANSI carriage control characters and ensures that the RECFM is FB rather than FBA. OMIT specifies that reformatted output records with ‘0’ or ‘9’ in position 81 (header or trailer records) and a sequence number in positions 82-83 greater than 1 (second and subsequent header or trailer records), are omitted. OUTREC keeps only positions 1-80 for the OUTFIL output records, thus removing the identifier byte and sequence number we added in positions 81-83 with the INREC statement (we do not want these temporary fields in the OUTFIL output records).

Example: IFTHEN abbreviate a word from Input File

In the following example an INREC statement will be used to abbreviate each instance of ‘NEW JERSEY’ and ‘NEW YORK’ in a record when position 24 of the record contains a X’01’.

INREC IFTHEN=(WHEN=(24,1,BI,EQ,X’01’), 
FINDREP=(INOUT=(C’NEW JERSEY’,C’NJ’,C’NEW YORK’, C’NY’)))

For the input record: NEW YORK,ABC NEW JERSEY,XYZ,NEW YORK

The output record would contain: NY,ABC NJ,XYZ,NY

If the records are variable-length, the RDW of the record would be reduced to indicate the new length after the shorter literals are substituted. If the records are fixed-length, spaces would be appended to the end of the record to replace the deleted characters.

Example: IFTHEN to identify Beginning and End of HEADER and Report

INREC WHEN=GROUP can be used with BEGIN to identify a header record starting a group and END to identify a trailer record ending a group. PUSH extends each record by placing the date and register number from the header record at the end of each record in the group, followed by a 5-byte group number and a 3-byte record sequence number. This enables all the records in a group to be sorted together.

INREC IFTHEN=(WHEN=GROUP,BEGIN=(1,1,CH,EQ,C’H’), 
              END=(1,1,CH,EQ,C’T’), 
              PUSH=(21:8,10,31:3,4,35:ID=5,SEQ=3))

If desired, a simple report can be created using OUTFIL IFTHEN to identify each different record type, format it appropriately, and remove the data added by PUSH. SECTIONS is used to generate a report header for each transaction.

    OUTFIL SECTIONS=(35,5,HEADER3=(’ DATE REG# ID’),SKIP=L),
    IFTHEN=(WHEN=(1,1,CH,EQ,C’H’), FOR HEADER RECORDS:
    BUILD=(21,10,2X,3,4,2X,3,4,2X,35,5)), 
    *      DATE, REG NUM, TRAN ID
    IFTHEN=(WHEN=(1,1,CH,EQ,C’S’),
    BUILD=(C’SKU#: ’,3,6,C’ PRICE: $’,10,7,
           C’ QUANTITY: ’,18,3)),
    IFTHEN=(WHEN=(1,1,CH,EQ,C’T’), FOR TRAILER RECORDS:
    BUILD=(C’TOTAL: $’,3,7))

IFTHEN processing continues until:

  • All IFTHEN parameters have been processed.
  • A WHEN=(conditions) or WHEN=ANY sub parameter condition is satisfied and the HIT=NEXT sub parameter is not included.
  • Multiple output records are created with the / sub parameter. Under the OUTREC parameter of the OUTFIL control statement, see “[n]/” on page 2.91 for a complete description of the / sub parameter.

The following is an example of the IFTHEN parameter:

OUTREC IFTHEN=(WHEN=INIT,BUILD=(1,80,1,8,ZD,MUL,+107,DIV,+100,ZD)), 
       IFTHEN=(WHEN=(81,15,ZD,GT,+10000),OVERLAY=
                    (81:81,15,ZD,ADD,+0500,ZD),HIT=NEXT),  
       IFTHEN=(WHEN=(81,15,ZD,GT,+20000),OVERLAY=
                    (81:81,15,ZD,ADD,+2000,ZD),HIT=NEXT),  
       IFTHEN=(WHEN=ANY,OVERLAY=(96:C’*’,97:81,15,ZD,MUL,+15,DIV,+100)), 
       IFTHEN=(WHEN=NONE,OVERLAY=(97:81,15,ZD,MUL,+12,DIV,+100))

If desired, a simple report can be created using OUTFIL IFTHEN to identify each different record type, format it appropriately, and remove the data added by PUSH. SECTIONS is used to generate a report header for each transaction.

 OUTFIL SECTIONS=(35,5,HEADER3=(’ DATE REG# ID’),SKIP=L),  
        IFTHEN=(WHEN=(1,1,CH,EQ,C’H’),
        BUILD=(21,10,2X,3,4,2X,3,4,2X,35,5)),
        IFTHEN=(WHEN=(1,1,CH,EQ,C’S’),  
        BUILD=(C’SKU#: ’,3,6,C’ PRICE: $’,10,7, C’QUANTITY: ’,18,3)), 
        IFTHEN=(WHEN=(1,1,CH,EQ,C’T’),
        BUILD=(C’TOTAL: $’,3,7))

SYNCSORT Manual: Click Here                   JCL Blogs: Click Here

Scroll to Top