SEQNUM creates & inserts sequence numbers in the output record. The sequence numbers are assigned in the order in which the records are received for INREC processing. The length of the field can be from 1 to 16 bytes. It is in BI, PD, or ZD formats. A starting value and an increment can be specified for the field. RESTART clause reinitiates sequence numbering when the value in a specified field changes. You can restart the sequence number at the start value each time a specified input field (p,m) or parsed input field (%nn) changes.
Syntax: Starting position:SEQNUM,m,f,START=j,INCR=i,RESTART=(p,m)
Name | Description |
---|---|
Starting Position | Specifies the position from where the sequence number starts in the o/p file. |
SEQNUM | A keyword specifies the sequence number to be generated from the next available position of INREC. |
Length (m) | Length of the sequence number to be generated. |
Format (f) | Format of the sequence number to be generated. |
START=j | Specifies the sequence number starts with instead of 1. |
INCR=i | Specifies the number used to increment the start number by i instead of 1. |
Restart=(p, m) | Specifies the field starting position and length on which the sequence number restart |
Sample Syntax
OUTREC FIELDS=(SEQNUM,6,ZD,START=1000,INCR=50, RESTART=(21,5),1,60) OUTREC OVERLAY=(21:21,4,ZD,TO=PD,LENGTH=4, 2:5,8,HEX,45:C'',32,4,C'',81:SEQNUM,5,ZD)
SEQNUM Examples with Explanation
Input New York Albany California Morgan Hill New York Buffalo Arizona Tuscon California San Jose New York Poughkeepsie Arizona Phoenix California Davis New York Armonk
Scenario: If you wanted to sort the records by the State field in positions 1-15 and by the City field in positions 16-30, and add a third field with a sequence number starting from 1000 and increment by 10, you could use START=1000 and INCR=10 as shown in the following statements:
SORT FIELDS=(1,30,CH,A) OUTREC OVERLAY=(32:SEQNUM,5,ZD,START=1000,INCR=10)
Output Arizona Phoenix 01000 Arizona Tuscon 01010 California Davis 01020 California Morgan Hill 01030 California San Jose 01040 New York Albany 01050 New York Armonk 01060 New York Buffalo 01070 New York Poughkeepsie 01080
- SORT FIELDS=(1,30,CH,A) – Input file sorted first and writes to the output.
- OUTREC OVERLAY=(32:SEQNUM,5,ZD,START=1000,INCR=10) – Generates the sequence number of length 5 from 32th byte. The sequence number starts at 1000 and incremented by 10 each time.
Scenario: If you wanted to start the sequence number over at 1000 again each time the State changed, you could add RESTART=(1,15) as shown in the following statements:
SORT FIELDS=(1,30,CH,A) OUTREC OVERLAY=(32:SEQNUM,5,ZD,START=1000,INCR=10, RESTART=(1,15))
Output Arizona Phoenix 01000 Arizona Tuscon 01010 California Davis 01000 California Morgan Hill 01010 California San Jose 01020 New York Albany 01000 New York Armonk 01010 New York Buffalo 01020 New York Poughkeepsie 01030
Note that the sequence number starts over at 1000 again each time the State changes.
Scenario: If you specify an IFTHEN clause with a sequence number, the sequence number is only incremented for the subset of records selected by that IFTHEN clause. For example, if you specified the following statements for the input records shown previously:
OPTION COPY OUTREC IFTHEN=(WHEN=(1,15,CH,EQ,C'Arizona'), OVERLAY=(32:SEQNUM,2,ZD)), IFTHEN=(WHEN=(1,15,CH,EQ,C'California'), OVERLAY=(32:SEQNUM,4,ZD)), IFTHEN=(WHEN=(1,15,CH,EQ,C'New York'), OVERLAY=(32:SEQNUM,3,ZD))
Output New York Albany 001 California Morgan Hill 0001 New York Buffalo 002 Arizona Tuscon 01 California San Jose 0002 New York Poughkeepsie 003 Arizona Phoenix 02 California Davis 0003 New York Armonk 004
Scenario: If SEQNUM is used in multiple IFTHEN clauses, the sequence number will be incremented for each record that satisfies the IFTHEN clause, that is, a separate SEQNUM counter will be kept for each IFTHEN clause. For example
Input RECORD A 1 RECORD B 1 RECORD B 2 RECORD C 1 RECORD A 2 RECORD C 2 RECORD B 3 RECORD D 1
OUTREC IFTHEN=(WHEN=(8,1,CH,EQ,C'A'), OVERLAY=(15:SEQNUM,4,ZD)), IFTHEN=(WHEN=(8,1,CH,EQ,C'B'), OVERLAY(16:SEQNUM,4,ZD)), IFTHEN=(WHEN=NONE,OVERLAY=(17:SEQNUM,4,ZD))
Output RECORD A 1 0001 RECORD B 1 0001 RECORD B 2 0002 RECORD C 1 0001 RECORD A 2 0002 RECORD C 2 0002 RECORD B 3 0003 RECORD D 1 0003
Different SEQNUM counters for the ‘A’ record, ‘B’ record, and NONE records.
Scenario: The below example illustrates how a sequence number can be generated, how values in one numeric or date format can be converted to another format, and how a lookup table can be used.
OPTION COPY,Y2PAST=1985 INREC FIELDS=(SEQNUM,4,BI, 8,5,ZD,TO=PD, 31,2,PD,TO=FI,LENGTH=2, 15,6,Y2TP, 25,3,CHANGE=(1,C’L92’,X’01’,C’M72’,X’02’,C’J42’,X’03’), NOMATCH=(X’FF’))
Explanation
The reformatted input records will look as follows:
Position | Contents |
1-4 | A binary sequence number that starts at 1 and increments by 1. |
5–7 | A PD field containing the converted ZD field from input positions 8 through 12. |
8–9 | An FI field containing the converted PD field from input positions 31 through 32. |
10–14 | A P’yyyymmdd’ date field containing the C’yymmdd’ date field from input positions 15-20 transformed according to the specified century window of 1985-2084. |
15 | A BI field containing X’01’, X’02’, X’03’ or X’FF’ as determined by using a lookup table for the input field in positions 25-27. |
The SORT statement can now refer to the “sort” field in the reformatted input records. The OUTREC statement is used to restore the records to their original format.
Scenario: This example illustrates how you can use the OVERLAY parameter with OUTFIL to add sequence numbers at the end of your records and to convert uppercase EBCDIC characters to lowercase EBCDIC characters in certain columns, without affecting the rest of the record.
OPTION COPY OUTFIL OMIT=(56,6,CH,EQ,C’******’), OVERLAY=(121:SEQNUM,8,ZD,56:56,6,TRAN=UTOL)
Explanation
The input data set has RECFM=FB and LRECL=120. The output data set will have RECFM=FB and LRECL=128.
The OMIT parameter removes records that have asterisks in positions 56-61.
121:SEQNUM,8,ZD in the OVERLAY parameter adds an 8-byte sequence number in positions 121-128 of every record. The LRECL is increased from 120 to 128 to hold the sequence number.
56:56,6,TRAN=UTOL in the OVERLAY parameter converts uppercase EBCDIC characters in positions 56-61 to lowercase EBCDIC characters in positions 56-61.
Only the two overlaid fields are changed; all of the other data in the records is unaffected.
Scenario: This example illustrates how a sequence number can be generated, how numeric and date values can be edited, and how a lookup table can be used.
OPTION COPY,Y2PAST=1985 OUTREC FIELDS=(SEQNUM,8,ZD,START=1000,INCR=100, 11:8,4,PD,M12, 31:15,4,Y2V(/), 51:2,1,CHANGE=(3, X’01’,C’L92’,X’02’,C’M72’,X’03’,C’J42’), NOMATCH=(C’???’))
Explanation
Position | Contents |
1-8 | A zoned decimal sequence number that starts at 1000 and increments by 100. |
11-20 | A CH field containing the PD field from input positions 8 through 11 was edited according to the M12 edit mask. |
31-40 | A C’yyyy/mm/dd’ date field containing the P’yymmdd’ date field from input positions 15-18 transformed according to the specified century window of 1985-2084. |
51–53 | A CH field containing C’L92′, C’M72′, C’J42′ or C’???’ as determined by using a lookup table for the input field in position 2. |
Scenario: This example shows how you can use three input files, each with a header record (‘HDR’), detail records (‘DTL’), and a trailer record (‘TRL’), and create an output file with one header record with the current date, the sorted detail records, and one trailer record with the current date.
//S1 EXEC PGM=SORT //SYSOUT DD SYSOUT=A //SORTIN DD DSN=INPUT.FILE1,DISP=SHR // DD DSN=INPUT.FILE2,DISP=SHR // DD DSN=INPUT.FILE3,DISP=SHR //SORTOUT DD DSN=OUTPUT.FILE,DISP=(NEW,CATLG,DELETE), // SPACE=(CYL,(5,5)),UNIT=SYSDA //SYSIN DD* INREC IFTHEN=(WHEN=(1,3,CH,EQ,C’HDR’), OVERLAY=(6:YDDD=(D4/),81:C’0’,82:SEQNUM,2,ZD)), IFTHEN=(WHEN=(1,3,CH,EQ,C’TRL’), OVERLAY=(11:YDDD=(D4/),81:C’9’,82:SEQNUM,2,ZD)), IFTHEN=(WHEN=NONE, OVERLAY=(81:C’1’)) SORT FIELDS=(81,1,CH,A,8,5,CH,A) OUTFIL REMOVECC, OMIT=(81,1,SS,EQ,C’0,9’,AND,82,2,ZD,GT,+1), OUTREC=(1,80) /*
Explanation
INREC statement. The first IFTHEN WHEN=(logexp) clause identifies and operates on header records (‘HDR’ in positions 1-3); OVERLAY puts today’s date in the form ‘ddd/yyyy’ in positions 6-13, adds a ‘0’ in position 81, adds a ZD sequence number in positions 82-83 and does not affect the rest of the record.
The second IFTHEN WHEN=(logexp) clause identifies and operates on trailer records (‘TRL’ in positions 1-3); OVERLAY puts today’s date in the form ‘ddd/yyyy’ in positions 11-18, adds a ‘9’ in position 81, adds a ZD sequence number in positions 82-83 and does not affect the rest of the record.
The IFTHEN WHEN=NONE clause identifies and operates on detail records (not ‘HDR’ or ‘TRL’ in positions 1-3); OVERLAY adds a ‘1’ in position 81 and does not affect the rest of the record.
DFSORT extends the reformatted input records from 80 bytes to 83 bytes to accommodate the identifier byte added in position 81 and the sequence number added in positions 82-83.
The ‘0’, ‘1’ or ‘9’ identifier byte added in position 81 allows us to sort the header records (‘0’) first, followed by the detail records (‘1’), and then the trailer records (‘9’). The sequence number added in positions 82-83 will allow us to keep only the first header record and the first trailer record. The sequence number will be 1 for the first header record, 2 for the second header record and 3 for the third header record. Likewise, the sequence number will be 1 for the first trailer record, 2 for the second trailer record and 3 for the third trailer record. Since the sequence number is not specified for the detail records, it will be blank.
SORT statement. FIELDS specifies an ascending 1-byte character control field at position 81 (the identifier byte added by INREC), and an ascending 5-byte character control field starting at position 8 (the key for the detail records).
OUTFIL statement. REMOVECC removes the ANSI carriage control characters and ensures that the RECFM is FB rather than FBA. OMIT specifies that reformatted output records with ‘0’ or ‘9’ in position 81 (header or trailer records) and a sequence number in positions 82-83 greater than 1 (second and subsequent header or trailer records), are omitted. OUTREC keeps only positions 1-80 for the OUTFIL output records, thus removing the identifier byte and sequence number we added in positions 81-83 with the INREC statement (we do not want these temporary fields in the OUTFIL output records).