Summing records
Summing records
Summary
- The SUM control statement used to specify one or more numeric fields that are to be summed whenever records have equally ordering control fields.
- The control fields are specified on the SORT statement.
The data formats allowed to specify on the SUM statement are -
- binary (BI)
- fixed-point (FI)
- packed decimal (PD)
- zoned decimal (ZD)
- floating-point (FL)
Only control fields from control statements (from SORT statement) are allowed while using the summary fields. The groups of same control field contents of the summary fields are summed. The groups of same control field are often called "duplicate" records.
Example -
Scenario - SUM the marks of duplicates at department level from the file.
Input File - MTHUSER.SORT.INPUT01
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
********************************* Top of Data **********************************
00001 student1 dept1 095
00003 student3 dept2 070
00004 student4 dept1 090
00005 student5 dept2 083
00002 student2 dept3 088
******************************** Bottom of Data ********************************
Input Record Layout -
01 INPUT-REC.
05 STD-ID PIC X(05).
05 FILLER PIC X(05).
05 STD-NAME PIC X(15).
05 FILLER PIC X(05).
05 STD-DEPT PIC X(10).
05 FILLER PIC X(05).
05 STD-MARKS PIC 9(03).
05 FILLER PIC X(32).
Job -
//Job-card
//**********************************************************************
//* SORT SUM STATEMENT
//**********************************************************************
//STEP01 EXEC PGM=SORT
//SORTIN DD DSN=MTHUSER.SORT.INPUT01,DISP=SHR
//SORTOUT DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//SYSIN DD *
SORT FIELDS=(30,10,CH,A)
SUM FIELDS=(45,3,ZD)
/*
**************************** Bottom of Data ****************************
Output -
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
********************************* TOP OF DATA **********************************
00001 student1 dept1 185
00003 student3 dept2 153
00002 student2 dept3 088
******************************** BOTTOM OF DATA ********************************
Explaining Example -
- As per requirement, department level duplicates needs to be summed up. So the STD-DEPT position, length, format required for SORT FIELDS. STD-DEPT start from 30th position of length 10 and type is alphanumeric as per declaration. So use CH.
- The SORT condition for the above requirement with all the data gathered is
- The above sort condition specifies all the record should sort with the department as key.
- Below SUM condition specifies to sum the marks of duplicate recrods with the key field as EMP-DEPT
- The output would have the first records of duplicates with the marks sum of all the records which are having same key.
SORT FIELDS=(30,10,CH,A)
SUM FIELDS=(45,3,ZD)