SYNCSORT -
SORT ORDER
SORTIN - INREC- INCLUDE- - SORT and SUM =0MIT-OUTREC-SORTOUT
HYPERLINK "https://www.ibmmainframer.com/jcl-tutorial/jcl-sort-inrec-fields/"https://
www.ibmmainframer.com/jcl-tutorial/jcl-sort-inrec-fields/
//STEP01 EXEC PGM=SORT
//SORTIN DD DSN=userid.SORT.INPUT,DISP=SHR
//SORTOUT DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//SYSIN DD *
SORT FIELDS=(1,3,ZD,A)
INREC FIELDS=(1,20,X,25,6,X,SEQNUM,4,ZD)
/*
INREC - It reformats, add, delete the input record and place it in the output file record. Here 1 to 20
char is copied to output file. Then ‘x’ means one space is added in the output record at 21st position.
The SEQNUM means that while copying from input file, what is the actual physical row number of that
particular record will be placed in that SEQNUM. If a record is located in the 5th row in input file, then
this SEQNUM will be written as 005 in output file.It is not the running sequence number
——————
Overwrite input record content.
//STEP01 EXEC PGM=SORT
//SORTIN DD DSN=userid.IBMMF.INPUT,DISP=SHR
//SORTOUT DD DSN=userid.IBMMF.OUTPUT,DISP=SHR
//SYSPRINT DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//SYSIN DD *
OPTION COPY
INREC OVERLAY=(47:1,6)
/*
In the input file, the content in position 1,6 is overwritten to the position 47,6 and then copied to the
output file. INREC OVERLAY operation is used in order to rewrite data in input file before copying to
output.
Backing up a file.
//STEP001 EXEC PGM=IEBGENER
//SYSPRINT DD SYSOUT=*
//SYSIN DD DUMMY
//SYSOUT DD SYSOUT=*
//SYSUT1 DD DSN=userid.IBMMF.INPUT,DISP=SHR
//SYSUT2 DD DSN=userid.IBMMF.OUTPUT,
// DISP=(NEW,CATLG,DELETE),
// DCB=*.SYSUT1,SPACE=(CYL,(50,1),RLSE)
IEBGENER copies the file in SYSUT1 to file in SYSUT2. Please note that file in SYSUT2 takes the same
DCB as that of the SYSUT1 in this example.
———————-
Example 9: Conditional Processing in SORT.
//STEP001 EXEC PGM=SORT
//SORTIN DD *
123456789012345 ---> Column
HEADRselect
DETAL
TRIALselect
/*
//SORTOUT DD DSN=userid.IBMMF.OUTPUT,
// DISP=(NEW,CATLG,DELETE),
// DCB=*.SYSUT1,SPACE=(CYL,(50,1),RLSE)
//SYSPRINT DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//SYSIN DD *
INREC IFTHEN=(WHEN=(6,1,CH,NE,C' '),BUILD=(1:1,15),
IFTHEN=(WHEN=(6,1,CH,EQ,C'
'),BUILD=(1:1,5,7:C'RECORD')
OPTION COPY
/*
Based on the 6th position of the file, the BUILD of output file varies. If 6th position is SPACES, then
text "RECORD" is appended to input record. Else, the input record is written to output, as-is.
———————-
Example 10: File Comparison using SORT utility.
//STEP001 EXEC PGM=SORT
//MAIN DD *
1000
1001
1003
1005
//LOOKUP DD *
1000
1002
1003
//MATCH DD DSN=userid.IBMMF.MATCH,DISP=OLD
//NOMATCH1 DD DSN=userid.IBMMF.NOMATCH1,DISP=OLD
//NOMATCH2 DD DSN=userid.IBMMF.NOMATCH2,DISP=OLD
//SYSOUT DD SYSOUT=*
//SYSIN DD *
JOINKEYS F1=MAIN,FIELDS=(1,4,A)
JOINKEYS F2=LOOKUP,FIELDS=(1,4,A)
JOIN UNPAIRED,F1,F2
REFORMAT FIELDS=(?,F1:1,4,F2:1,4)
OPTION COPY
OUTFIL FNAMES=MATCH,INCLUDE=(1,1,CH,EQ,C'B'),BUILD=(1:2,4)
OUTFIL
FNAMES=NOMATCH1,INCLUDE=(1,1,CH,EQ,C'1'),BUILD=(1:2,4)
OUTFIL
FNAMES=NOMATCH2,INCLUDE=(1,1,CH,EQ,C'2'),BUILD=(1:2,4)
/*
JOINKEYS specifies the field on which the two files are compared.
Program logic
If Record (a) < Record (b)
put Record (a) into Unmatched file and read a new record from File A. and start
compare again.
2. If Record (a) > Record (b)
put Record (b) into Unmatched file and read a new record from File B. and start
compare again.
3. If Record (a) = Record (b)
Make sure than one of these records <> High Value (to make sure we haven't
reached the end of file).. If it's equal, its end of program Else put the record into
Matched file and fetch a new record of both the files A and B.
REFORMAT FIELDS=? places 'B' (matched records), '1' (present in file1, but not in file2), or '2' (present
in file2 but not in file1) in the 1st position of the output BUILD.
JOIN UNPAIRED does a full outer join on the two files.
The output will be:
MATCH File
1000
1003
NOMATCH1 File
1001
1005
NOMATCH2 File
1002
The same functionality can be achieved using ICETOOL also.
————-
Adding Header/Trailer to output file.
//STEP01 EXEC PGM=SORT
//SORTIN DD *
indata1
indata2
indata3
/*
//SORTOUT DD DSN=userid.IBMMF.OUTPUT,DISP=SHR
//SYSPRINT DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//SYSIN DD *
SORT FIELDS=COPY
OUTFIL REMOVECC,
HEADER1=(1:C'HDR',11:X'020200825C'),
TRAILER1=(1:C'TRL',TOT=(11,9,PD,TO=PD,LENGTH=5))
/*
TOT calculates the number of records in the input file. HDR and TRL are added as identifiers to
header/trailer, which is user defined and can be customised as per the users' needs.
————————
FINDREP: Also CHANGE does the same in SORT
Reformat each record by doing various types of find and replace operations. Example:
INREC FINDREP=(IN=C'Mr.',OUT=C'Mister')
—————————————-
————
Example 1: Formating a file(USING INREC)
//SYSIN DD *
SORT FIELDS=COPY
INREC FIELDS=(7:2,5,20:10,3)
/*
Explanation:
SORT FIELDS=COPY It is for copy records to output file
INREC FIELDS=(7:2,5,20:10,3) - Here we have two formattings,
7:2,5 - data at 2nd position of input file with length 5 copied to 7th position of output file
20:10,3 - data at 10th position of input file with length 3 copied to 20th position of output file
IFTHEN clauses:
Reformat different records in different ways by specifying how build, overlay, find/replace, or group
operation items are applied to records that meet given criteria. IFTHEN clauses let you use
sophisticated conditional logic to choose how different record types are reformatted. Example:
INREC IFTHEN=(WHEN=(1,5,CH,EQ,C'TYPE1'),
BUILD=(1,40,C'**',+1,TO=PD)),
IFTHEN=(WHEN=(1,5,CH,EQ,C'TYPE2'),
BUILD=(1,40,+2,TO=PD,X'FFFF')),
IFTHEN=(WHEN=NONE,OVERLAY=(45:C’NONE')
—————————
JCL: -
----+----1----+---2---+---3---+---4---+---5---+----6---+----7--
***************************** Top of Data
***************************
//MTHUSRR JOB
(123),'MTHUSR',CLASS=A,MSGCLASS=A,MSGLEVEL=(1,1),
// NOTIFY=&SYSUID
//*
//STEP01 EXEC PGM=ICETOOL
//INDD DD DSN=MTHUSR.INPUT.DATASET,DISP=SHR
//OUTDD DD DSN=MTHUSR.OUTPUT.DATASET1,
// DISP=(NEW,CATLG,DELETE),
// SPACE=(TRK,(1,1),RLSE),UNIT=SYSDA,
// DCB=(DSORG=PS,RECFM=FB,LRECL=80,BLKSIZE=800)
//TOOLIN DD *
SORT FROM(INDD) TO(OUTDD) USING(CTL1)
/*
//CTL1CNTL DD *
SORT FIELDS=(50,2,CH,A)
/*
//TOOLMSG DD DSN=MTHUSR.OUTPUT.TOOLMSG1,DISP=SHR
//DFSMSG DD SYSOUT=*
//
**************************** Bottom of Data ***************
Explaining Solution: -
INDD - Specifies the ddname for input dataset.
OUTDD - Specifies the ddname for output dataset.
TOOLIN DD * - Specifies the ICETOOL statements for DFSORT.
CTL1CNTL DD * - Specifies the DFSORT statements for processing.
TOOLMSG - Specifies ICETOOL message data set.
DFSMSG - Specifies DFSORT message data set.
SORT FROM(INDD) TO(OUTDD) USING(CTL1) - Sorts the indd
records and copies to OUTDD based on the selection criteria in CTL1.
SORT FIELDS=(50,2,CH,A) - Sorts the records in ascending order
based on the data from 50th position of length 2.
————————