DFSORT OUTFIL Selecting and sampling by RRN

We can use the OUTFIL statement to select or sample records based on their Relative Record Number (RRN), which refers to the position of a record in the input dataset. The implicit relative record numbers start from 1 for the first record and n for the last record (n is the total number of records).

Syntax -

//SYSIN DD *
   SORT FILEDS=...
	OUTFIL FNAMES=DDname-n,STARTREC=starting_RRN,
	   ENDREC=ending_RRN
/*
STARTREC=starting_RRNSpecifies the first record to be included in the output, based on its relative position.
ENDREC=ending_RRNSpecifies the last record to be included in the output, based on its relative position.

Examples -

Scenario1 - Selecting Records by Relative Record Number (RRN).

//SYSIN   DD  *
  SORT FIELDS=COPY
  OUTFIL FNAMES=SORTOUT,STARTREC=5,ENDREC=10
/*

Starts output from the 5th record and stops after the 10th record. Only records from 5 to 10 (inclusive) are written to the output file SORTOUT.

Scenario2 - Sampling Every nth Record.

//SYSIN   DD  *
  SORT FIELDS=COPY
  OUTFIL FNAMES=SORTOUT,SAMPLE=3
/*

Selects every 3rd record from the input file. Only records with RRNs of 3, 6, 9, etc., are written to the output file.

Scenario3 - Combining Sampling and Record Range.

//SYSIN   DD  *
  SORT FIELDS=COPY
  OUTFIL FNAMES=SORTOUT,STARTREC=10,ENDREC=50,SAMPLE=5
/*

Selects records between the 10th and 50th records. From those selected records, every 5th record is written to the output, starting from the 10th record.