PARSE and IFTHEN PARSE are new options that allow you to extract variable position/length fields into fixed-length parsed fields defined as %nn fields. It gives you powerful new capabilities for handling variable fields such as delimited fields, comma-separated values (CSV), tab-separated values, blank-separated values, keyword-separated fields, null-terminated strings, and many other types.
The PARSE parameter is optional except in WHEN=INIT if BUILD or OVERLAY is not specified. It is used to extract variable position and variable-length fields from records and place the resultant data into fixed-length parsed fields. PARSE cannot be used in a WHEN=GROUP clause.
PARSE may be used to search for each keyword and extract the data into fixed-length fields in a reconstructed record. In this example, some of the keywords in certain records may be missing. This normally would cause the cursor to be moved to the end of the record so that the search for the next keyword fails. But, by using PARSE with an IFTHEN WHEN=INIT separately for each field, this problem can be avoided because the cursor is reset to the beginning of the record for each new PARSE.
PARSE using IFTHEN Explanation
IFTHEN sort is one of the most useful features of Syncsort programming language. It has the ability to do conditional logic, which can be described conceptually as IF something, THEN something.
The IFTHEN parameters must be specified such that the WHEN sub-parameters are in the following order:
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.
WHEN=(conditions) and WHEN=ANY
WHEN=NONE
For detailed information on syntax and a better understanding of IFTHEN usage click here
A file has records with name and address information in a keyword format
PARSE using IFTHEN Example
Example: PARSE Name and Address from input data.
NAME1=GEORGE;NAME2=BUSH;ADDR1=OVAL OFFICE;ADDR2=1600 PENNSYLVANIA AVE;CITY=WASH NAME1=WILLIAM;MI=J;NAME2=CLINTON;ADDR1=15 OLD HOUSE LN;CITY=CHAPPAQUA;STATE=NY NAME1=GEORGE;MI=H;NAME2=BUSH;CITY=HOUSTON;STATE=TX NAME1=BROWN;MI=S;NAME2=MILLS;CITY=LOSANGLES;STATE=CA NAME1=SAM;MI=H;NAME2=DOHN;CITY=WOODLAND HILLS;STATE=CA NAME1=ANIL;MI=H;NAME2=OJHA;CITY=ROCKYHILL;STATE=CT
//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 *  *USE WHEN=INIT ONCE FOR EACH KEYWORD IN DATA INREC IFTHEN=(WHEN=INIT, PARSE=(%1=(STARTAFT=C'NAME1=',ENDBEFR=C';',FIXLEN=12))), IFTHEN=(WHEN=INIT, PARSE=(%2=(STARTAFT=C'MI=',ENDBEFR=C';',FIXLEN=1))), IFTHEN=(WHEN=INIT, PARSE=(%3=(STARTAFT=C'NAME2=',ENDBEFR=C';',FIXLEN=12))), IFTHEN=(WHEN=INIT, PARSE=(%4=(STARTAFT=C'ADDR1=',ENDBEFR=C';',FIXLEN=24))), IFTHEN=(WHEN=INIT, PARSE=(%5=(STARTAFT=C'ADDR2=',ENDBEFR=C';',FIXLEN=24))), IFTHEN=(WHEN=INIT, PARSE=(%6=(STARTAFT=C'CITY=',ENDBEFR=C';',FIXLEN=12))), IFTHEN=(WHEN=INIT, PARSE=(%7=(STARTAFT=C'STATE=',ENDBEFR=C';',FIXLEN=2))), *AFTER EXTRACTING THE DATA FOR EACH KEYWORD, ARRANGE IT IN FIXED COLUMNS IFTHEN=(WHEN=NONE, BUILD=(1:%1,14:%2,16:%3,29:%4,54:%5,79:%6,92:%7)) SORT FIELDS=(92,2,CH,A) * SORT BY "STATE" //*
Output
GEORGE BUSH OVAL OFFICE 1600 PENNSYLVANIA AVE WASH WILLIAM J CLINTON 15 OLD HOUSE LN CHAPPAQUA NY GEORGE H BUSH HOUSTON TX BROWN S MILLS LOSANGLES CA SAM H DOHN WOODLAND HILLS CA ANIL H OJHA ROCKYHILL CT