TSO-ISPF JCL COBOL VSAM DB2 CICS IMS-DB Tools Articles Forum Quiz Interview Q&A

JCL - SORT SORT Fields


The SORT control statement must be used when a sorting file is performed. This statement describes the control fields in the input records on which the program sorts. A SORT statement can also be used to specify a copy records.

If multiple positions of sorting specified, the order of priority is from left to right.

The sorting orders can be either Ascending or descending.

Syntax:

SORT FIELDS=(starting position, length, data format, A/D)

Where,

Name Description
starting positionStarting position of field to be compared
Length Length of the field to be compared
data formatFormat of the field like CH, PD, BI, ZD etc,.
A/D Specifies the sorting order, A- Ascending, B- Descending

Example 1: SORT a record in input file by one field.

INPUT FILE:

12345678901234567890 ---> Column
KALAIA  123 RASAN
SRINIV  198 ASAN
KALAIA  023 RASAN
REVATH  111 ISAMBATH
SRINIV  098 ASAN
SANKAR  222 ICHELLA
GUNASE  333 ELAN

Below code is to sort the record in input file based on employee name(starting position-1,Length-6). CH - Represents charater. A - Represents order the record in Ascending.

SORT JCL:

//STEP01    EXEC PGM=SORT
//SYSOUT    DD SYSOUT=*
//SORTIN    DD DSN=userid.SORT.INPUT.FILE,DISP=SHR  ---> Input file
//SORTOUT   DD DSN=userid.SORT.OUTPUT.FILE,         ---> Output file
//       DISP=(NEW,CATLG,DELETE),UNIT=SYSDA,
//       SPACE=(CYL,(1,4),RLSE),
//       DCB=(RECFM=FB,LRECL=80,BLKSIZE=0)
//SYSIN     DD *
  SORT FIELDS=(1,6,CH,A)                            ---> Sort statements
/*

When you execute the above JCL, it produces the following result −

OUTPUT FILE:

12345678901234567890 ---> Column
GUNASE  333 ELAN
KALAIA  123 RASAN
KALAIA  023 RASAN
REVATH  111 ISAMBATH
SRINIV  198 ASAN
SRINIV  098 ASAN
SANKAR  222 ICHELLA

Explanation:

Above SORT FIELDS sorted the recrods, depends on fields we have provided

Sort Field - 1,6,CH,A - field started at column 1 , its length is 6

In the above example, CH- means character.

There are many character format is available, We mostly use below formats.

  1. CH - Character
  2. ZD - Signed zoned decimal
  3. PD - Signed packed decimal
  4. BI - Unsigned binary

'A' means Ascending order. We can also sort the record by descending using 'D'.


Example 2: SORT a record in input file by two field.

If you see input file, it has same employee name for more than one record(e.g. KALAIA, SRINIV).

let us add another field in the sort field. so that the same employee name will be sorted again by employee id(field started at column 9 , its length is 3)

SORT JCL:

//STEP01    EXEC PGM=SORT
//SYSOUT    DD SYSOUT=*
//SORTIN    DD DSN=userid.SORT.INPUT.FILE,DISP=SHR  ---> Input file
//SORTOUT   DD DSN=userid.SORT.OUTPUT.FILE,         ---> Output file
//       DISP=(NEW,CATLG,DELETE),UNIT=SYSDA,
//       SPACE=(CYL,(1,4),RLSE),
//       DCB=(RECFM=FB,LRECL=80,BLKSIZE=0)
//SYSIN     DD *
  SORT FIELDS=(1,6,CH,A,9,3,ZD,A)                   ---> Sort statements
/*

When you execute the above JCL, it produces the following result −

OUTPUT FILE:

12345678901234567890 ---> Column
GUNASE  333 ELAN
KALAIA  023 RASAN
KALAIA  123 RASAN
REVATH  111 ISAMBATH
SRINIV  098 ASAN
SRINIV  198 ASAN
SANKAR  222 ICHELLA


If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal