INREC Inserting Sequence Numbers


We can use the INREC statement to insert sequence numbers into records before sorting, merging, or copying them. Inserting sequence numbers is particularly useful when -

  • The records need to get back to the original position after their usage.
  • To uniquely identify records or add a running total to records during processing.

Syntax -

//SYSIN DD * 
    SORT FIELDS=COPY
	INREC FIELDS=(…,SEQNUM, length, format,..)
/*
SEQNUMThe keyword for inserting a sequence number.
lengthSpecifies the length (in bytes) of the sequence number.
formatSpecifies the format of the sequence number. Common formats include: ZD (Zoned Decimal), PD (Packed Decimal) and BI (Binary).

Examples -


Scenario1 - Basic Sequence Number in Zoned Decimal Format.

INREC FIELDS=(1,10,SEQNUM,5,ZD)

The first 10 bytes of the input record are retained (from position 1 to 10) and a 5-byte zoned decimal sequence number is appended to each record. The sequence number starts at 1 and increments by 1 for each record.

Scenario2 - Sequence Number with a Starting Value and Increment in Packed Decimal Format.

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

The first 20 bytes of the input record are retained and a 4-byte packed decimal sequence number is added to each record. The sequence number starts at 1000 and increments by 10 for each record.