OUTREC Reformatting records with IFTHEN
The OUTREC statement with the IFTHEN parameter allows us to apply conditional reformatting to records. IFTHEN is used to check for specific conditions in the records and apply formatting changes such as inserting data, replacing fields, or modifying records based on those conditions.
Syntax -
//SYSIN DD *
SORT FIELDS=...
OUTREC IFTHEN=(WHEN=(...),BUILD=(field_specifications))
/*
IFTHEN clause is classifed as 5 types -
- WHEN=INIT - used to apply BUILD, FINDREP or OVERLAY items to all input records. WHEN=INIT and WHEN=GROUP options are processed before any other WHEN starts processing.
- WHEN=GROUP - used to multiply fields, identifiers and sequence numbers within specified groups of records.
- WHEN=(logexp) - used to apply BUILD, FINDREP or OVERLAY items to the input records that meet specified criteria. It is satisfied when the logical expression evaluates as true.
- WHEN=ANY - used to apply additional BUILD, FINDREP or OVERLAY items to the input records if they satisfied the criteria for any of the preceding WHEN=(logexp) clauses.
- WHEN=NONE - used to apply additional BUILD, FINDREP or OVERLAY items to the input records that did not meet the criteria for any of the preceding WHEN=(logexp) clauses.
Examples -
Scenario1 - Conditionally modifying records based on field value.
//SYSIN DD *
SORT FIELDS=COPY
OUTREC IFTHEN=(WHEN=(1,3,CH,EQ,C'ABC'),BUILD=(1,20,C'FOUND_ABC'))
/*
If the first 3 bytes of the record are 'ABC', the output will include the first 20 bytes from the input followed by the string 'FOUND_ABC'.
Scenario2 - Conditionally reformatting numeric fields.
//SYSIN DD *
SORT FIELDS=COPY
OUTREC IFTHEN=(WHEN=(10,4,ZD,GT,1000),BUILD=(1,10,C'OVER_1000')),
IFTHEN=(WHEN=(10,4,ZD,LE,1000),BUILD=(1,10,C'UNDER_1000'))
/*
If the 4-byte zoned decimal field starting at position 10 is greater than 1000, the output will include the first 10 bytes of the input followed by the string 'OVER_1000'.
If the value is less than or equal to 1000, the output will include the first 10 bytes followed by 'UNDER_1000'.
Scenario3 - Conditionally modifying records based on field value (including WHEN=NONE).
//SYSIN DD *
SORT FIELDS=COPY
OUTREC IFTHEN=(WHEN=(1,3,CH,EQ,C'ABC'),BUILD=(1,20,C'FOUND_ABC')),
IFTHEN=(WHEN=NONE,BUILD=(1,20,C'NO_MATCH'))
/*
If the first 3 bytes of the record are 'ABC', the output will include the first 20 bytes from the input followed by the string 'FOUND_ABC'.
If the condition is not met (i.e., the first 3 bytes are not 'ABC'), the output will include the first 20 bytes from the input followed by the string 'NO_MATCH'.