The DFSORT OPTION STOPAFT=n tells the sort step to stop after writing n records to the output. Once the nth record has been written to SORTOUT (and any OUTFIL), DFSORT stops reading and processing; remaining input is not read. STOPAFT is useful when you want to limit the size of the output—for example to create a sample of sorted data, to run a quick test with a cap on records, or to feed a downstream process that accepts only a fixed number of records. This page explains how STOPAFT works, how it interacts with SKIPREC and filtering, and when to use it.
When you specify OPTION STOPAFT=n, DFSORT runs the sort (or merge or copy) and writes records to SORTOUT and any OUTFIL outputs. As soon as it has written the nth record, it stops. It does not read more input, does not write more output, and does not continue the sort/merge phase for the rest of the file. So the output dataset(s) contain at most n records. If the input (after SKIPREC and INCLUDE/OMIT) produces fewer than n records, you get that smaller number; STOPAFT is an upper limit. The value n must be a positive integer in the range supported by your product.
Code STOPAFT as part of the OPTION statement in SYSIN. You can combine it with other options. Example:
12OPTION STOPAFT=10000 SORT FIELDS=(1,10,CH,A)
To create a sample of 1000 records while preserving order of equal keys:
12OPTION EQUALS,STOPAFT=1000 SORT FIELDS=(20,4,PD,D)
To skip one header and then limit output to 500 records:
12OPTION SKIPREC=1,STOPAFT=500 SORT FIELDS=(1,80,CH,A)
It is important to remember that STOPAFT counts output records. Suppose you have 100,000 input records and use INCLUDE so that only 40% pass (40,000 records). If you specify STOPAFT=1000, DFSORT writes 1000 records to output and stops. To produce those 1000 output records, it may have to read several thousand input records (because many are omitted by INCLUDE). So STOPAFT does not mean "read only n input records"; it means "write at most n output records." Similarly, with SUM FIELDS=NONE you might collapse 10 input records per key into 1 output record; STOPAFT=500 then means 500 output records, which could represent many more input records. This distinction matters when you estimate runtime or when you reason about how much input was actually read.
| Aspect | Behavior |
|---|---|
| What is limited | Number of records written to output |
| When applied | After sort/merge and SUM; when writing to SORTOUT/OUTFIL |
| Input | Only enough input is read to produce n output records |
| Typical use | Sampling, testing, top-N, capping output size |
SKIPREC is applied first: the first n input records are skipped. Then the remaining input is filtered (INCLUDE/OMIT), reformatted (INREC), sorted or merged, optionally summed (SUM), and written (OUTREC/OUTFIL). STOPAFT applies when writing: as soon as n output records have been written, processing stops. So the order is: skip (SKIPREC) → filter → sort/merge → write; STOPAFT stops the run when the write count reaches n. If you use both SKIPREC=1 and STOPAFT=100, you skip one input record and then produce at most 100 output records from the rest. If you use INCLUDE and STOPAFT=100, you get 100 records that passed the INCLUDE condition; how many input records were read depends on how many had to be read to get 100 that pass.
One benefit of STOPAFT is that DFSORT can stop reading input once it has produced n output records. For a sort, that may mean DFSORT does not have to read the entire input file—it can stop the sort phase once it has written n records (the exact internal behavior is product-dependent). So for very large inputs, STOPAFT can reduce both I/O and sortwork compared with sorting the whole file and then taking the first n records in a separate step. You get the "first n" in sorted order without processing the remainder.
When you use OUTFIL to write to multiple datasets or with SPLIT, STOPAFT typically applies to the total output: once n records have been written across SORTOUT and the OUTFIL outputs (or in the way your product defines it), processing stops. The exact semantics—whether n is per output or total—can vary by product. Check your DFSORT documentation. In common implementations, STOPAFT limits the main SORTOUT (and possibly each OUTFIL) so that you do not get more than n records in the primary output; OUTFIL copies may be limited the same way. If in doubt, run a small test with OUTFIL and STOPAFT and verify the record counts.
Sort by bytes 1–10 and write only the first 5000 records:
12OPTION STOPAFT=5000 SORT FIELDS=(1,10,CH,A)
Copy (no sort), skip first record, and limit output to 100 records:
12OPTION COPY,SKIPREC=1,STOPAFT=100 OUTREC FIELDS=(1,80)
Imagine you are filling a box with toys from a big pile, and you sort them by color first. STOPAFT is like saying: "Put only 10 toys in the box and then stop." So you sort and add toys until the box has 10; then you stop, even if there are more toys in the pile. You do not have to sort or touch the rest of the pile.
1. What does OPTION STOPAFT=n do?
2. You sort a million-record file but only need the first 5000 for a test. What do you use?
3. Does STOPAFT count input records or output records?
4. Can you combine STOPAFT with SKIPREC?
5. What happens if the input has fewer records than STOPAFT after filtering?