MainframeMaster

Sequence Numbering in INREC and OUTREC

Sequence numbering in DFSORT adds a sequential number to each record—1, 2, 3, … or a custom start and step (e.g. 1000, 1010, 1020). You use the SEQNUM keyword in INREC or OUTREC, with a length and format (ZD, PD, or BI). Optional START= and INCR= set the starting value and increment; RESTART=(position,length) resets the sequence when a control field (e.g. group id) changes, so you get 1, 2, 3 within each group. The important difference: INREC assigns sequence numbers before the sort (numbers follow input order), and OUTREC assigns them after the sort (numbers follow output order). This page explains syntax, when to use INREC vs OUTREC, and how to use RESTART for group-based numbering.

INREC Processing
Progress0 of 0 lessons

Why Add Sequence Numbers?

Sequence numbers give each record a unique position identifier: useful for line numbers in reports, for restoring original order after processing, or for numbering within groups (e.g. item 1, 2, 3 within each order). DFSORT can generate these numbers automatically with SEQNUM—no need for a separate program. You choose where to put the number (INREC or OUTREC), the length and format (e.g. 5-byte zoned decimal), and optionally the start value, increment, and a control field to restart the sequence (e.g. by group).

SEQNUM Syntax and Parameters

In INREC, you add SEQNUM as an item in FIELDS= or BUILD=. In OUTREC, you typically use OVERLAY to place the sequence number at a specific output position. The common parameters are:

SEQNUM parameters
ParameterMeaningExample
SEQNUMKeyword that requests a sequence numberRequired
lengthLength in bytes of the sequence number field4, 5, 6
formatZD (zoned decimal), PD (packed decimal), or BI (binary)ZD for display
START=Starting value (default 1)START=1000
INCR=Increment per record (default 1)INCR=10
RESTART=Restart sequence when (position,length) control field value changesRESTART=(1,5)

length must be large enough to hold the maximum sequence value (e.g. 5 bytes for ZD can hold 99999). ZD is zoned decimal (one digit per byte, readable when printed). PD is packed decimal (compact). BI is binary. START= and INCR= default to 1 if omitted. RESTART= is used when you want the sequence to reset at each control break (e.g. when bytes 1–5 change).

INREC: Sequence Before Sort

When you use SEQNUM in INREC, the sequence number is assigned in input record order. The first input record gets the first number (e.g. 1 or START=), the second gets the next (e.g. 2 or START+INCR), and so on. Then the sort runs. So after the sort, the sequence numbers no longer necessarily increase in output order—they still reflect the original input order. Use INREC SEQNUM when you need to preserve or recover that original order (e.g. to sort back by sequence number later) or when the number is part of a key that is used before or during the sort.

text
1
2
INREC FIELDS=(1,10,SEQNUM,5,ZD,11,70) SORT FIELDS=(1,10,CH,A)

Here the first 10 bytes are kept, then a 5-byte ZD sequence number (1, 2, 3, …), then bytes 11–80 from input. So each record gets a number in input order. The sort then orders by the first 10 bytes; the sequence number remains as assigned and still reflects input sequence.

OUTREC: Sequence After Sort

When you use SEQNUM in OUTREC, the sequence number is assigned after the sort, in output record order. So the first record in the sorted output gets 1 (or START=), the second gets 2, and so on. Use OUTREC SEQNUM when you want line numbers or position numbers that reflect the final order (e.g. report line 1, 2, 3).

text
1
2
SORT FIELDS=(1,10,CH,A) OUTREC FIELDS=(1,20,21:SEQNUM,5,ZD)

The sort runs first. Then OUTREC copies the first 20 bytes and places a 5-byte ZD sequence number at position 21. So the output has positions 1–20 from the sorted record and 21–25 as the sequence number (1, 2, 3, … in output order). Syntax like 21:SEQNUM,5,ZD specifies the output position for the sequence number.

START= and INCR=

By default, the sequence starts at 1 and increments by 1. You can change this with START= and INCR=. For example, SEQNUM,5,ZD,START=1000,INCR=10 produces 1000, 1010, 1020, … for each record. This is useful when you need a specific starting value (e.g. to avoid overlapping with another range) or a step other than 1 (e.g. for spacing or for alignment with another numbering scheme).

text
1
INREC FIELDS=(1,20,SEQNUM,4,PD,START=1000,INCR=10,21,60)

The first 20 bytes are kept, then a 4-byte packed decimal sequence number (1000, 1010, 1020, …), then bytes 21–80. So the record length is 20 + 4 + 60 = 84 bytes. PD is compact; use ZD if you need the number to be human-readable in a report.

RESTART=: Reset Sequence by Group

When you want the sequence to restart at each group (e.g. 1, 2, 3 for group A, then 1, 2, 3 for group B), use RESTART=(position,length). You specify the position and length of a control field. When that field's value changes from one record to the next, the sequence number is reset (e.g. to START= or 1). The data should be sorted by that control field so that all records in the same group are contiguous; otherwise the restart happens whenever the value changes, which may not be what you want.

text
1
2
SORT FIELDS=(1,5,CH,A) OUTREC OVERLAY=(6:SEQNUM,4,ZD,RESTART=(1,5))

Records are sorted by the first 5 bytes (e.g. order id). OUTREC overlays position 6 with a 4-byte ZD sequence number. RESTART=(1,5) means: when the value in bytes 1–5 changes, reset the sequence. So within each order id you get 1, 2, 3, … and the next order id starts again at 1.

Format: ZD, PD, BI

ZD (zoned decimal): One digit per byte in EBCDIC. Good for display and reports; the number prints as you expect. Length 5 gives you up to 5 digits (e.g. 99999). PD (packed decimal): Compact; half-bytes. Good when the sequence number is used internally or as a sort key and you want to save space. BI (binary): Fullword or halfword binary. Use when you need a binary integer for a downstream program. Choose ZD for human-readable output, PD or BI for compact or programmatic use.

Explain It Like I'm Five

Imagine you have a stack of cards and you want to write a number on each: 1 on the first, 2 on the second, 3 on the third. Sequence numbering is the machine that does that. If we number the cards before we shuffle them (INREC), then after we shuffle, the numbers are mixed up—but we still know which card was first, second, third in the original stack. If we number the cards after we shuffle (OUTREC), then the first card in the new stack gets 1, the second gets 2, so the numbers match the new order. And if we have groups (like red cards and blue cards), we can say "start counting from 1 again whenever the color changes"—that's RESTART.

Exercises

  1. You need report line numbers 1, 2, 3, … in the order the report is written. Should you use SEQNUM in INREC or OUTREC? Why?
  2. You need to add a sequence number so that after sorting you can restore the original input order by sorting on that number. INREC or OUTREC? Why?
  3. Write an INREC FIELDS= that keeps bytes 1–50 and appends a 5-byte ZD sequence number starting at 100 with increment 5.
  4. What does RESTART=(1,10) do if the file is sorted by bytes 1–10?

Quiz

Test Your Knowledge

1. When are sequence numbers assigned if you use SEQNUM in INREC?

  • After the sort
  • Before the sort (so numbers reflect input order)
  • Only for the first record
  • Random order

2. When are sequence numbers assigned if you use SEQNUM in OUTREC?

  • Before the sort
  • After the sort (so numbers reflect final output order)
  • Only in INREC
  • Only for COPY

3. What does RESTART=(position,length) do with SEQNUM?

  • Restarts the job
  • Restarts the sequence number (e.g. to 1 or START=) when the value in the specified field changes (control break)
  • Restarts the sort
  • Length of record

4. What does INCR= do in SEQNUM?

  • Increment key
  • Step value: the sequence number increases by this amount for each record (e.g. INCR=10 gives 1000, 1010, 1020)
  • Input record count
  • Include condition

5. Which format (ZD, PD, BI) is commonly used for a human-readable sequence number in a report?

  • BI only
  • PD only
  • ZD (zoned decimal) is common for display; PD and BI are valid for internal use
  • CH