Summing Data
Summing Data Example
Scenario - Sum the marks at department level.
Input File - MATEPK.SORT.INPUT
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8 00001 student1 dept1 095 00003 student3 dept2 070 00004 student1 dept1 090 00005 student3 dept2 083 00002 student2 dept3 088
JCL -
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
//MATEPKD JOB (123),'MTH',NOTIFY=&SYSUID
//*
//STEP01 EXEC PGM=SORT
//SORTIN DD DSN=MATEPK.SORT.INPUT01,DISP=SHR
//SORTOUT DD DSN=MATEPK.MERGE.OUTPUT,
// DISP=(NEW,CATLG,DELETE),UNIT=SYSDA,
// SPACE=(CYL,(1,4),RLSE),
// DCB=(RECFM=FB,LRECL=80,BLKSIZE=0)
//SYSOUT DD SYSOUT=*
//SYSIN DD *
SORT FIELDS=(30,10,CH,A)
SUM FIELDS=(45,03,ZD)
/*
Output -
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8 00001 student1 dept1 185 00003 student3 dept2 153 00002 student2 dept3 088
Explaining Example -
- As a first step, need to get the position of departmentto add it in SORT condition.So the STD-DEPT starting position is 30 and length is 10.
- From the requirement,the department is alphabetic. So the type is character (CH).
SORT FIELDS=(30,10,CH,A)
- department level marks needs to sum. So the STD-MARKS position, length, format required for SUM FIELDS. STD-MARKS start from 45th position of length 3 and type is numeric as per declaration. So use ZD.SUM FIELDS=(45,03,ZD)
- the output would have the first records of duplicates with the sum of marks at the STD-MARKS position and unique records with original marks from input.