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 –
- DFSORT – This is a utility product provided by IBM.
- 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
5 -> Length from the starting position
CH -> Format type is Character
A -> Ascending order
Note :
In place of 'A', you can also give 'B', if you want to sort the records based on Descending order
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
SUM FIELDS=NONE -> Remove duplicate records
Note :
In place of SORT FIELDS=COPY, you can also give a certain sort of position like - SORT FIELDS=(5,4,ZD,D)
Here, ZD is Zoned Decimal
JCL Interview Question and Answers
1. Which of the following are valid with respect to SORT in File-aid? (choose 2)
- We can sort records with only one field.
- For RRDS and BDAM files the SORT command sorts records in relative record number (RRN or RBA) order.
- We cannot sort a keyed file on a field other than the key field.
- The default parameter for SORT is KEYS for a keyed file. Thus the commands SORT and SORT KEYS are the same.
- We cannot sort a keyed file on a field other than the key field.
- 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