SORT – FINDREP, IFTHEN and INREC
-
FINDREP
Using SORT, you can FIND a value and REPLACE it with another value.
Suppose you have your input file as below:
RAJESH TML
RAMS TML
SUNIL TML
SURESH TML
And you want your output file in this format
RAJESH TPT
RAMS TPT
SUNIL TPT
SURESH TPT
Your SORT card would look like below:
OPTION COPY
OUTREC FINDREP=(IN=C’TML’,OUT=C’TPT’)
Or
OPTION COPY
OUTREC FINDREP=(INOUT=(C’TML’,C’TPT’)
FINDREP | is used to find and replace the input record |
IN | Tells the field that needs to be found |
OUT | Tells the field that needs to be replaced with |
INOUT | Specifies both find and replace strings in order |
-
IFTHEN
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=GROUP: Use one or more WHEN=GROUP clauses to propagate fields, identifiers and sequence numbers within groups of records.
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.
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.
Sample JCL:
Input File
RAJESH
RAMS
SURI
SUNIL
And you want your output file in this format
RAJESH FRIEND BTECH
RAMS FRIEND MCA
SURI FRIEND MSC
SUNIL FRIEND BE
Your SORT card would look like below:
OPTION COPY
OUTREC IFTHEN=(WHEN=INIT,BUILD=(1,7,C’FRIEND’,20X),
IFTHEN=(WHEN=(1,7,CH,EQ,C’RAJESH’,OVERLAY=(15:C’BTECH’)),
IFTHEN=(WHEN=(1,7,CH,EQ,C’RAMS’,OVERLAY=(15:C’MCA’)),
IFTHEN=(WHEN=(1,7,CH,EQ,C’SURI’,OVERLAY=(15:C’MSC’)),
IFTHEN=(WHEN=NONE,OVERLAY=(15:C’BE’)))
Example to use WHEN=GROUP
Suppose you want to write only the records between HDR and TRL into the output file.
Your input file looks like below
HDR VARUN
001 SRI
002 RAJESH
TRL RAMS
TMP SURI
HDR SUNIL
001 CHIRU
TRL PAVAN
TP1 PRAVEEN
OPTION COPY
OUTREC IFTHEN=(WHEN=GROUP,BEGIN=(1,3,CH,EQ,C’HDR’),
END=(1,3,CH,EQ,C’TRL’),PUSH=(21:ID=1))
OUTFIL INCLUDE=(21,1,CH,NE,C’ ‘),BUILD=(1,20)
PUSH overlays a 1-byte ID character at position 21 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
HDR VARUN 1
001 SRI 1
002 RAJESH 1
TRL RAMS 1
TMP SURI
HDR SUNIL 2
001 CHIRU 2
TRL PAVAN 2
TP1 PRAVEEN
Your output file will be like below due to the OUTFIL statement
OUTFIL INCLUDE=(21,1,CH,NE,C’ ‘),BUILD=(1,20)
HDR VARUN
001 SRI
002 RAJESH
TRL RAMS
HDR SUNIL
001 CHIRU
TRL PAVAN
- INREC
INREC reformats the records before they are sorted, so the SORT and SUM statements must refer to the reformatted records as they will appear in the output data set.
If you want to sort your input file based on bytes 11-15 and write only the bytes 11-20 into the output file.
INREC FIELDS=(11,10)
SORT FIELDS=(1,5,CH,A)
OUTREC FIELDS=(1,10)
Instead of
SORT FIELDS=(11,5,CH,A)
OUTREC FIELDS=(11,10)
——————————————————————————————————–
In United States, If you would like to Earn Free Stocks, Credit card Points and Bank account Bonuses, Please visit My Finance Blog
——————————————————————————————————–
You may also like to look at:
Working on Mainframes – Is Change to a different technology necessary
Pingback: SORT – PARSE – READ EXCEL (CSV) FILE IN MAINFRAMES | F1 for Mainframe