## Module 8: JCL Sort & Merge

### SORT FIELDS

The SORT command is used to specify sorting criteria and features.

Syntax of SORT command:

SORT FIELDS= (sort-key1, sort-key2,…., sort-keyN)

Where, each sort-key consist of below four paramaters:-

Let’s understand SORT with help of example

Input file:-

----+----1----+----2----+----3----+----4----+----5 88888JOHN PURCHASING 08000 11111AKSHAY HR 10000 55555SMITH R&D 25000 44444STEVE ADMIN 20000 33333VIJAY FINANCE DEPT 24000 77777VIJAY FINANCE DEPT 30000 44444STEVE HR 20000

Requirement: Sort above file on columns 1 to 5 in ascending order

JCL Code:

//SORTSTEP EXEC PGM=SORT //SORTIN DD DSN=DEPT.EMPL.DATA.INPUT, // DISP=SHR //SORTOUT DD DSN=DEPT.EMPL.DATA.OUTPUT1, // DISP=(,CATLG,DELETE), // DCB=(RECFM=FB,LRECL=50,BLKSIZE=0), // UNIT=TEST,SPACE=(CYL,(50,10),RLSE) //SYSOUT DD SYSOUT=* //SYSPRINT DD SYSOUT=* //SYSIN DD * SORT FIELDS=(1,5,CH,A) /*

Output file:-

----+----1----+----2----+----3----+----4----+----5 11111AKSHAY HR 10000 33333VIJAY FINANCE DEPT 24000 44444STEVE HR 20000 44444STEVE ADMIN 20000 55555SMITH R&D 25000 77777VIJAY FINANCE DEPT 30000 88888JOHN PURCHASING 08000

#### SORT COPY

The SORT control statement with FIELDS=COPY defines the application as a COPY application.

SORT FIELDS=COPY {,SKIPREC=n} {,STOPAFT=n} {,CKPT | CHKPT}

Where,
SKIPREC indicates the number of records to be skipped
STOPAFT indicates the number of records to process
CKPT/CHKPT indicates a checkpoint is desired at the end of volume of a SORTOUT data set when OUTFIL is not used.

Below are few more requirement which can be addressed using SORT command:

Example 1:

Requirement:To sort input file on column 1 to 5 on in descending order and 31 to 45 in ascending order
SYSIN Control card:

Solution 1: SORT FIELDS=(1,5,CH,D,31,15,CH,A) Solution 2: SORT FIELDS=(1,5,D,31,15,A),FORMAT=CH

Example 2:

Requirement: To copy all records from SORTIN to SORTOUT without sorting it
SYSIN Control card:

SORT FIELDS=COPY

Example 3:

Requirement: To copy records from row number 5 to 7 as it is from SORTIN to SORTOUT
SYSIN Control card:

SORT FIELDS=COPY,SKIPREC=5,STOPAFT=2

Example 4:

Requirement: To concatenate two or more sorted or unsorted datasets into one sorted dataset on column 1 to 5 in ascending order
SYSIN Control card:
IMP NOTE: In SORTIN card three datasets are concatenated

//SORTSTEP EXEC PGM=SORT //SORTIN DD DSN=DEPT.EMPL.DATA.INPUT,DISP=SHR // DD DSN=DEPT.EMPL.DATA.INPUT1,DISP=SHR // DD DSN=DEPT.EMPL.DATA.INPUT2,DISP=SHR //SORTOUT DD DSN=DEPT.EMPL.DATA.OUTPUT1, // DISP=(,CATLG,DELETE), // DCB=(RECFM=FB,LRECL=50,BLKSIZE=0), // UNIT=TEST,SPACE=(CYL,(50,10),RLSE) //SYSOUT DD SYSOUT=* //SYSPRINT DD SYSOUT=* //SYSIN DD * SORT FIELDS=(1,5,CH,A) /*