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

JCL - SORT SUM Fields


The SUM control statement specifies that, whenever two records are found with equal sort or merge control fields, the contents of their summary fields are to be added, the sum is to be placed in one of the records, and the other record is to be deleted.

SUM fields also used to eliminate the duplicate record in input file.

Let see examples below.

INPUT FILE:

1234567890123456789012345678901234567890 ---> Column
KALAIA  12345678901234567 RASAN
SRINIV  19876543210987654 ASAN
KALAIA  02345678901234567 RASAN
REVATH  11111111111111111 ISAMBATH
SRINIV  09876543210987654 ASAN
SANKAR  22222222222222222 ICHELLA
GUNASE  33333333333333333 ELAN

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,3,CH,A)                          ---> Sort statements
    SUM FIELDS=NONE
/* 

OUTPUT FILE:

1234567890123456789012345678901234567890 ---> Column
GUNASE  33333333333333333 ELAN
KALAIA  12345678901234567 RASAN
REVATH  11111111111111111 ISAMBATH
SANKAR  22222222222222222 ICHELLA
SRINIV  19876543210987654 ASAN

Explanation:

Above syntax of SORT sorted the recrods, depends on fields we have provided in Sort fields.

FIRST FIELD - 1,3,CH,A - first key started at col 1 , its length is 3

In the above example, 'CH' means character; 'A' means Ascending order

SUM FIELDS=NONE removes duplicates on fields specified in SORT FIELDS. In the above example, employee name(first 3 character) is in the field position 1,3. The output file will contain the unique employee numbers sorted in ascending order.


Example 2: Input file has one or more records with same employee number. Write unique records to output.

INPUT FILE:

1234567890123456789012345678901234567890 ---> Column
KALAIA  22222222222222222 RASAN
SRINIV  11111111111111111 ASAN
KALAIA  02345678901234567 RASAN
REVATH  11111111111111111 ISAMBATH
SRINIV  09876543210987654 ASAN
SANKAR  22222222222222222 ICHELLA
GUNASE  33333333333333333 ELAN

SORT JCL:

//STEP010  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=(9,17,ZD,A)
  SUM FIELDS=NONE
/*

OUTPUT FILE:

1234567890123456789012345678901234567890 ---> Column
KALAIA  02345678901234567 RASAN
SRINIV  09876543210987654 ASAN
SRINIV  11111111111111111 ASAN
KALAIA  22222222222222222 RASAN
GUNASE  33333333333333333 ELAN

SUM FIELDS=NONE removes duplicates on fields specified in SORT FIELDS. In the above example, employee number is in the field position 9,17. The output file will contain the unique employee numbers sorted in ascending order.


Example 3: SORT JCL to Copy eliminated duplictes into another file.

This can be achived by using SUM FIELDS XSUM OPTION. Let'us see an example below,

SORT JCL:

//STEP010  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)
//SORTXSUM DD DSN=userid.SORT.OUTPUT.XSUM,
//            DISP=(NEW,CATLG,DELETE),UNIT=SYSDA,
//            SPACE=(CYL,(1,4),RLSE),
//            DCB=(RECFM=FB,LRECL=80,BLKSIZE=800)
//SYSIN    DD *
    SORT FIELDS=(1,3,CH,A)
    SUM FIELDS=NONE,XSUM
/*

INPUT FILE:

1234567890123456789012345678901234567890 ---> Column
KALAIA  22222222222222222 RASAN
SRINIV  11111111111111111 ASAN
KALAIA  02345678901234567 RASAN
REVATH  11111111111111111 ISAMBATH
SRINIV  09876543210987654 ASAN
SANKAR  22222222222222222 ICHELLA
GUNASE  33333333333333333 ELAN

OUTPUT FILE:

1234567890123456789012345678901234567890 ---> Column
GUNASE  33333333333333333 ELAN
KALAIA  22222222222222222 RASAN
REVATH  11111111111111111 ISAMBATH
SANKAR  22222222222222222 ICHELLA
SRINIV  11111111111111111 ASAN

SORTXSUM FILE:

KALAIA  02345678901234567 RASAN
SRINIV  09876543210987654 ASAN

EXPLANATION

SORT FIELDS=(1,3,CH,A) - Input file will be sorted depending up on the field specified.

1,3,CH,A - field starting position is 1 and length 3, comparing type character, sorting in ascending order.

SUM FIELDS=NONE,XSUM - SUM FIELDS=NONE means it will eliminate duplicates; XSUM options will copy all records eliminated in sort process will copy to another data set defined in SORTXSUM ddname file.


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