
STOPAFT SKIPREC are two powerful control parameters in IBM DFSORT that allow you to precisely control which records are processed during sorting, merging, or copying operations. These parameters help optimize performance by reducing the amount of data processed and enable selective record processing without requiring complex programming logic. STOPAFT and SKIPREC are optional parameters and can be used together or separately in a SORT JCL.
SKIPREC=n instructs DFSORT to skip the first n records from the input dataset before starting the sort, merge, or copy operation. For example, if you want to sort only the first 1000 records of a file, you can specify STOPAFT=1000 in your SORT JCL. This parameter is particularly useful for:
- Skipping header records in reports.
- Avoiding processing unwanted records at the beginning of a file.
- Starting processing from a specific record position.
STOPAFT=n tells DFSORT to stop processing after n records have been accepted for the operation. This parameter helps:
- Limit the number of records processed.
- Improve performance by avoiding unnecessary processing.
- Create subsets of large files for testing or analysis.
Syntax and Usage
SORT FIELDS=COPY,SKIPREC=n,STOPAFT=n OR OPTION COPY,SKIPREC=n,STOPAFT=n
Parameter Specifications
- SKIPREC=n: Skip the first n records (n can be 0 to 2,147,483,647).
- STOPAFT=n: Stop after processing n records (n can be 0 to 2,147,483,647).
- Both parameters can be used together or separately.
- They can be specified on either the SORT or OPTION statement.
Processing Order and Logic
When both SKIPREC and STOPAFT are specified, DFSORT processes them in the following order:
- First: SKIPREC is applied – the first n records are skipped.
- Then: STOPAFT is applied – processing stops after n records are accepted.
For example, if you specify SKIPREC=5 and STOPAFT=10, DFSORT will:
- Skip the first 5 records (records 1–5).
- Process the next 10 records (records 6–15).
- Ignore all remaining records (records 16 onwards).
Details:
- STOPAFT and SKIPREC work with SORT, MERGE, and COPY operations.
- You can use numeric or symbolic values for n (using SYMDEF).
- If fewer records exist than STOPAFT, SORT processes all available records.
- If fewer records exist than SKIPREC, SORT starts with the first available record.
- If both are used, SKIPREC is applied first.
- If neither is specified, all records are processed by default.
Optimization Tips
- Use SKIPREC and STOPAFT together for maximum efficiency when processing subsets.
- Place them on the OPTION statement for better readability and IBM-recommended best practice.
- Combine with INCLUDE/OMIT for more advanced filtering logic.
- Use STOPAFT=1 for quick testing with a single record.
STOPAFT SKIPREC Examples
STOPAFT using SORT
STOPAFT stops processing after ‘N’ number of records while sorting. You can specify it using either the SYSIN control statements or as a PARM parameter. Note: If both are provided, the PARM value takes precedence.
//STEP001 EXEC PGM=SORT //$ORTPARM DD * STOPAFT=100 /* OR //STEP001 EXEC PGM=SORT,PARM='STOPAFT=100' OR //STEP001 EXEC PGM=SORT //SYSPRINT DD SYSOUT=* //SYSOUT DD SYSOUT=* //SYSIN DD * SORT FIELDS=COPY,STOPAFT=100 /*
STOPAFT using ICETOOL
//STEP001 EXEC PGM=ICETOOL //TOOLMSG DD SYSOUT=* //DFSMSG DD SYSOUT=* //IN1 DD DSN=XXXXXX.YYYYY.INPUT,DISP=SHR //OUT1 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) //TOOLIN DD * COPY FROM(IN1) TO(OUT1) USING(CTL1) /* //CTL1CNTL DD * OPTION STOPAFT=100 /*
You can also use the SELECT operator with ICETOOL to replicate the behavior of STOPAFT and SKIPREC. This allows for more advanced filtering and record-range handling.
Using SELECT with ICETOOL
//SORTJOB JOB ... //TOOLSTEP EXEC PGM=ICETOOL //TOOLIN DD * SELECT FROM(IN) TO(OUT) SKIP(10) FIRST(1000) SORT FROM(IN) TO(TEMP) USING(CTL1) OUTFIL FNAMES=OUT,INCLUDE=(1,1,CH,EQ,C'1') /* //CTL1 DD * SORT FIELDS=COPY /*
In this example, ICETOOL SELECT skips the first 10 records and processes the next 1000. A COPY sort is then executed, and OUTFIL includes only records that start with ‘1’. This combination provides advanced control over input sampling and conditional processing.
SKIPREC using SORT
SKIP ‘N’ number of records while SORTING
//STEP001 EXEC PGM=SORT
//SYSPRINT DD SYSOUT=*
//SYSOUT DD SYSOUT=*
.............
.............
//SYSIN DD *
SORT FIELDS=COPY,SKIPREC=N
/*
STOPAFT SKIPREC: SKIP ‘X’ and STOP after ‘Y’ number of records while SORTING
//SYSIN DD *
SORT FIELDS=COPY,SKIPREC=X,STOPAFT=Y /*
//*
Alternative Approaches
Using OUTFIL with STARTREC and ENDREC
Instead of SKIPREC and STOPAFT, you can use OUTFIL with STARTREC and ENDREC:
//STEP1 EXEC PGM=SORT //SYSOUT DD SYSOUT=* //SYSPRINT DD SYSOUT=* //SORTIN DD DSN=USER.INPUT.FILE,DISP=SHR //SORTOUT DD DSN=USER.OUTPUT.FILE, // DISP=(NEW,CATLG,DELETE), // UNIT=SYSDA,SPACE=(CYL,(1,1)), // DCB=(RECFM=FB,LRECL=80) //SYSIN DD * SORT FIELDS=COPY OUTFIL FNAMES=SORTOUT,STARTREC=5,ENDREC=7 /*
Key Differences:
-
STOPAFT: DFSORT stops reading records when the count is reached (more efficient)
-
ENDREC: DFSORT reads all records but only outputs the specified range (less efficient)
Using OUTFIL with ACCEPT
For limiting output records in OUTFIL:
//STEP1 EXEC PGM=SORT
//SYSOUT DD SYSOUT=*
//SYSPRINT DD SYSOUT=*
//SORTIN DD DSN=USER.INPUT.FILE,DISP=SHR
//SORTOUT DD DSN=USER.OUTPUT.FILE,
// DISP=(NEW,CATLG,DELETE),
// UNIT=SYSDA,SPACE=(CYL,(1,1)),
// DCB=(RECFM=FB,LRECL=80)
//SYSIN DD *
SORT FIELDS=COPY
OUTFIL FNAMES=SORTOUT,STARTREC=5,ACCEPT=3
/*
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=1000parameter 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.
Error Handling and Considerations
Common Issues
SKIPREC value exceeds input records: If SKIPREC is greater than the number of input records, no records will be processed.
STOPAFT=0: Results in no output records.
Large SKIPREC values: May cause performance issues if the file is very large.
Best Practices
Validate input file size before using large SKIPREC values.
Use STOPAFT for testing to avoid processing large files during development.
Combine with INCLUDE/OMIT for complex filtering requirements.
Monitor SYSPRINT output to verify the expected number of records are processed.
Performance Considerations
Efficiency Benefits
SKIPREC Benefits:
- Reduces processing time by eliminating unwanted records early.
- Reduces memory usage for sort operations.
- Minimizes I/O operations on skipped records.
STOPAFT Benefits:
- Stops reading input when the required number of records is processed.
- Particularly efficient for large files when only a subset is needed.
- Reduces CPU time and I/O operations.
Summary
STOPAFT and SKIPREC are essential parameters for controlling record processing in DFSORT operations. They provide:
- Efficient subset processing by limiting the amount of data processed.
- Performance optimization through reduced I/O and CPU usage.
- Flexibility in handling various data processing scenarios.
- Simplicity in implementation without requiring complex programming logic.
These parameters are particularly valuable for:
- Processing large files where only a subset is needed.
- Skipping header/trailer records in reports.
- Creating test datasets from production files.
- Implementing sampling strategies for data analysis.
By understanding and utilizing STOPAFT and SKIPREC effectively, you can create more efficient and maintainable JCL sort applications that process exactly the data you need, when you need it.
