## Module 8: JCL Sort & Merge

### SUM FIELDS

It is used to sum up numeric data in the records that have the same control field values, after sorting.

Syntax:-

Let’s have look at examples to understand

Example 1:-

Input file:-

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

Requirement: Sum up the expenses (field at position 46-50) if records are same

SYSIN Control card:

//SYSIN DD * SORT FIELDS=(1,45,CH,A) SUM FIELDS=(46,5,ZD) /*

Output file:

----+----1----+----2----+----3----+----4----+----5 11111AKSHAY HR 10000 44444STEVE HR 40000 55555SMITH R&D 25000 77777VIJAY FINANCE DEPT 54000 88888JOHN PURCHASING 16000

Explanation: In above case role of statement ‘SORT FIELDS=(1,45,CH,A) ’ is to sort the records in ascending order based on data at position 1-45. Next statement i.e. ‘SUM FIELDS=(46,5,ZD)‘ is used to indicate that data at position 45-50 is in zoned decimal format and should be added up if data at position 1-45 matches up

Example 2:-

Input file:-

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

Requirement: Remove duplicate records from input file

SYSIN Control card:

//SYSIN DD * SORT FIELDS=(1,50,CH,A) SUM FIELDS=NONE /*

Output file:

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

Example 3:-

Input file:-

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

Requirement: Identify records which has duplicate names at position (6-30) and the eliminated duplicate records should be moved to some other file

JCL code:

//SORTSTEP EXEC PGM=SORT //SORTIN DD DSN=DEPT.EMPL.DATA.INPUT20,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) //SORTXSUM DD DSN=DEPT.EMPL.DATA.DUPLCAT, // 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=(6,25,CH,A) SUM FIELDS=NONE,XSUM /*

OUTPUT File SORTOUT:-

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

OUTPUT File SORTXSUM (this is where eliminated records are saved):-

----+----1----+----2----+----3----+----4----+----5 88888JOHN PURCHASING 08000 44444STEVE HR 20000 77777VIJAY FINANCE DEPT 24000