MainframeMaster

STOPAFT

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.

OPTION Statement
Progress0 of 0 lessons

What STOPAFT Does

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.

Syntax and Placement

Code STOPAFT as part of the OPTION statement in SYSIN. You can combine it with other options. Example:

text
1
2
OPTION STOPAFT=10000 SORT FIELDS=(1,10,CH,A)

To create a sample of 1000 records while preserving order of equal keys:

text
1
2
OPTION EQUALS,STOPAFT=1000 SORT FIELDS=(20,4,PD,D)

To skip one header and then limit output to 500 records:

text
1
2
OPTION SKIPREC=1,STOPAFT=500 SORT FIELDS=(1,80,CH,A)

STOPAFT Counts Output, Not Input

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.

STOPAFT at a glance
AspectBehavior
What is limitedNumber of records written to output
When appliedAfter sort/merge and SUM; when writing to SORTOUT/OUTFIL
InputOnly enough input is read to produce n output records
Typical useSampling, testing, top-N, capping output size

When to Use STOPAFT

  • Sampling. You need a sample of the sorted file (e.g. first 1000 or 10,000 records) for validation or reporting. STOPAFT=n gives you exactly that without writing a separate program to read and stop.
  • Testing. During development you may want to run the same SORT control statements on a small subset of output. STOPAFT=100 or 1000 keeps the test run short and the output small.
  • Top-N. When you sort by a key and only need the "first N" records (e.g. top 100 by value), STOPAFT=N gives you those N records. The rest of the input is never fully processed, which can save time and sortwork for large files.
  • Capping output. A downstream application or file format may accept only a maximum number of records. STOPAFT ensures you never write more than that limit.

Interaction with SKIPREC and Filtering

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.

Performance and Sortwork

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.

STOPAFT with OUTFIL

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.

Examples

Sort by bytes 1–10 and write only the first 5000 records:

text
1
2
OPTION STOPAFT=5000 SORT FIELDS=(1,10,CH,A)

Copy (no sort), skip first record, and limit output to 100 records:

text
1
2
OPTION COPY,SKIPREC=1,STOPAFT=100 OUTREC FIELDS=(1,80)

Explain It Like I'm Five

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.

Exercises

  1. You have 2 million input records. You want the first 10,000 after sorting by position 15–20 (PD,D). What OPTION do you add, and does DFSORT read all 2 million input records?
  2. You use INCLUDE to keep only records where byte 1 is 'A'. Input has 100,000 records, 20,000 of which have 'A'. You set STOPAFT=5000. How many output records do you get, and roughly how many input records might DFSORT read?
  3. What is the difference between "stop after n input records" and "stop after n output records"? Which does STOPAFT do?
  4. When might STOPAFT improve job runtime compared with sorting the entire file and then using another tool to take the first n records?

Quiz

Test Your Knowledge

1. What does OPTION STOPAFT=n do?

  • Stops after reading n input records
  • Stops after writing n records to the output (SORTOUT/OUTFIL)
  • Stops after n seconds of processing
  • Stops after n control statements

2. You sort a million-record file but only need the first 5000 for a test. What do you use?

  • INCLUDE to keep only 5000 records
  • OPTION STOPAFT=5000
  • OMIT to drop records after 5000
  • OUTREC to truncate the file

3. Does STOPAFT count input records or output records?

  • Input records
  • Output records
  • Both; it stops when either reaches n
  • It counts control statements

4. Can you combine STOPAFT with SKIPREC?

  • No; they are mutually exclusive
  • Yes; SKIPREC skips the first n input records, then STOPAFT limits how many output records are written
  • Only with OPTION COPY
  • STOPAFT overrides SKIPREC

5. What happens if the input has fewer records than STOPAFT after filtering?

  • DFSORT abends
  • You get fewer output records than n; DFSORT completes normally
  • DFSORT waits for more input
  • STOPAFT is ignored