Example: Sort the input file based on the first 11 bytes and format the output for the first 10,000 records
//STEP001 EXEC PGM=SORT
//SYSPRINT DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//SYSUDUMP DD SYSOUT=*
//REPORT1 DD SYSOUT=*
//SORTIN DD DISP=SHR,DSN=XXXXXX.YYYYYY.INPFILE,
//SORTOUT DD DSN=XXXXXX.YYYYYY.OUTFILE,
// DISP=(NEW,CATLG,DELETE),
// SPACE=(TRK,(30,10),RLSE),
// UNIT=SYSDA,
// DCB=(RECFM=FB,LRECL=80,BLKSIZE=0,DSORG=PS)
//SORTWK01 DD UNIT=DISK,SPACE=(CYL,(20,5),RLSE)
//SORTWK02 DD UNIT=DISK,SPACE=(CYL,(20,5),RLSE)
//SORTWK03 DD UNIT=DISK,SPACE=(CYL,(20,5),RLSE)
//SYSIN DD *
SORT FIELDS=(01,11,BI,A),SKIPREC=1,STOPAFT=10000
OUTREC FIELDS=(001:001,35,
036:036,05,PD,EDIT=(TTTTTTTTT),
045:041,02,PD,EDIT=(TTT),
048:043,05,
053:048,03,PD,EDIT=(TTTTT),
058:051,03,PD,EDIT=(TTTTT),
063:054,08,PD,EDIT=(TTTTTTTTTTTTTTT))
/*
//
Explanation: The SKIPREC and STOPAFT parameters in the SORT control statement allow you to limit processing to specific portions of the input file. This helps in test runs, sampling, or selective sorting.
For instance, you may also use these in combination with filtering clauses like INCLUDE or OMIT to conditionally exclude records or with SUM to summarize data.
Example: Sort and Summarize While Skipping Records
SORT FIELDS=(1,10,CH,A)
SKIPREC=10
SUM FIELDS=(11,4,ZD)
Here, the SORT utility skips the first 10 records, sorts by the first 10 characters, and summarizes a 4-byte numeric field starting at position 11.
Example: Sort Variable-Length Records with SKIPREC and RECL
SORT FIELDS=(1,10,CH,A)
SKIPREC=5
INREC IFTHEN=(WHEN=INIT,OVERLAY=(1001:SEQNUM,8,ZD)),
IFTHEN=(WHEN=(1001,8,ZD,LE,1000),BUILD=(1,1000))
OUTREC BUILD=(1,1000)
RECL=1000
In this advanced example:
- The input file is variable-length.
- First 5 records are skipped using
SKIPREC=5. - A sequence number is added temporarily to control sorting range.
- The
RECL=1000 parameter ensures correct record length for variable input.
This technique is ideal when working with long or inconsistent record formats and you want to maintain tight control over what part of the file is processed.
Conclusion
Using JCL SORT with SKIPREC and STOPAFT gives you fine-grained control over file processing. Whether you need to process a subset of records, format output selectively, or handle variable-length datasets, these SORT parameters help optimize your mainframe jobs effectively.
Don’t forget to test thoroughly and validate the results with SYSPRINT and REPORT outputs to ensure your JCL does exactly what you intend.