SORT in JCL

SORT in JCL

Every JCL programmer must know an important process which is – SORT in JCL.

So, what exactly is SORT?

Suppose, you have an input file and you want to format the data in multiple ways.
For Example

  • Sort a particular field or position in ascending or descending order.
  • Removing the duplicate records from the file.
  • To find a bad record from the list of records.
  • Copy the input file by including or excluding a few/some records.
  • Merging the fields from the input.

The most commonly used SORT utilities in Mainframe are –

  1. DFSORT – This is a utility product provided by IBM.
  2. SYNCSORT – This is a utility product provided by SYNCSORT INC.

Note:

  • Both DFSORT and SYNCSORT perform SORT operation and it depends on your organization to select which product they want to take.
  • DFSORT has an inbuilt advanced utility – ICETOOL which is very powerful.

Syntax to sort a fixed position in a particular order:

SORT FIELDS=(starting position,Length,format ,order).

Examples of SORT in JCL

Suppose, we have an input file like below and we will perform various SORT operation on this input file

Input File

000003AKHKSDKJA UO6878932UIUYF8797 80999899999 8888888888888888888888888
000001KKKLJERER LKJLKJJJJJDF787878 76768886867 7677777777777777788687666
000002KJLJLKJSD HSKKKKKKKKDF876867 68668767867 6576576576666666666667557
000002KJLJLKJSD HSKKKKKKKKDF876867 68668767867 6576576576666666666667557
000007KJHKJHJKD TYTUTYUDTFS6757686 76576576564 7657666666666666666557556
000006JSDJSHDJG YYYYIUDYFISUYDI897 75786876876 7878688888886768676768677

1. How to sort the input file based on a particular position

Example to sort in ascending order

//MATEKSD JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID
//*
//* SORT PROGRAM BASED ON ASCENDING ORDER
//*
//SORT0100 EXEC PGM=SORT
//SYSPRINT DD SYSOUT=*
//SYSOUT   DD SYSOUT=*
//SYSDUMP  DD SYSOUT=*                                     
//SORTIN   DD DSN=MATEKS.TEST.SORTIN,DISP=SHR
//SORTOUT  DD DSN=MATEKS.TEST.SORTOUT,DISP=OLD
//SRTWK01  DD UNIT=SYSDA,SPACE=(TRK,(5,3))
//SORTWK01 DD SPACE=(TRK,(1,1),RLSE),UNIT=SYSDA            
//SORTWK02 DD SPACE=(TRK,(1,1),RLSE),UNIT=SYSDA            
//SORTWK03 DD SPACE=(TRK,(1,1),RLSE),UNIT=SYSDA
//SYSIN    DD *
           SORT FIELDS=(2,5,CH,A)
/*

Output

000001KKKLJERER LKJLKJJJJJDF787878 76768886867 7677777777777777788687666
000002KJLJLKJSD HSKKKKKKKKDF876867 68668767867 6576576576666666666667557
000002KJLJLKJSD HSKKKKKKKKDF876867 68668767867 6576576576666666666667557
000003AKHKSDKJA UO6878932UIUYF8797 80999899999 8888888888888888888888888
000006JSDJSHDJG YYYYIUDYFISUYDI897 75786876876 7878688888886768676768677
000007KJHKJHJKD TYTUTYUDTFS6757686 76576576564 7657666666666666666557556

2. How to remove duplicate records and write unique records

Example to remove duplicate records

//MATEKSD JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID
//*
//* SORT PROGRAM TO REMOVE DUPLCATE RECORDS
//*
//SORT0100 EXEC PGM=SORT
//SYSPRINT DD SYSOUT=*
//SYSOUT   DD SYSOUT=*
//SYSDUMP  DD SYSOUT=*                                     
//SORTIN   DD DSN=MATEKS.TEST.SORTIN,DISP=SHR
//SORTOUT  DD DSN=MATEKS.TEST.SORTOUT,DISP=OLD
//SRTWK01  DD UNIT=SYSDA,SPACE=(TRK,(5,3))
//SORTWK01 DD SPACE=(TRK,(1,1),RLSE),UNIT=SYSDA            
//SORTWK02 DD SPACE=(TRK,(1,1),RLSE),UNIT=SYSDA            
//SORTWK03 DD SPACE=(TRK,(1,1),RLSE),UNIT=SYSDA
//SYSIN    DD *                                            
   SORT FIELDS=COPY                                        
   SUM FIELDS=NONE
/* 
//*

Output

000001KKKLJERER LKJLKJJJJJDF787878 76768886867 7677777777777777788687666
000002KJLJLKJSD HSKKKKKKKKDF876867 68668767867 6576576576666666666667557
000003AKHKSDKJA UO6878932UIUYF8797 80999899999 8888888888888888888888888
000006JSDJSHDJG YYYYIUDYFISUYDI897 75786876876 7878688888886768676768677
000007KJHKJHJKD TYTUTYUDTFS6757686 76576576564 7657666666666666666557556

JCL Interview Question and Answers

1. Which of the following are valid with respect to SORT in File-aid? (choose 2)

  1. We can sort records with only one field.
  2. For RRDS and BDAM files the SORT command sorts records in relative record number (RRN or RBA) order.
  3. We cannot sort a keyed file on a field other than the key field.
  4. The default parameter for SORT is KEYS for a keyed file. Thus the commands SORT and SORT KEYS are the same.
  1. We cannot sort a keyed file on a field other than the key field.
  2. The default parameter for SORT is KEYS for a keyed file. Thus the commands SORT and SORT KEYS are the same.

2. When sorting the records, how can u erase the duplicate records?

SUM FIELDS = NONE (TO ERASE DUPLICATES)
SUM FIELDS = ALL (WITH THE DUPLI)
SUM FIELDS = (NONE, XSUM)
XSUM IS A DATASET CONTAINING DUPLICATE VALUES

Tutorials for all brains!