SUM statement


A SUM statement syntax shown below -

SUM FIELDS=(field starting position, field Length, field format_type)
Field1 starting positionStarting position of field1 to be compared
Field1 LengthLength of the field1 to be compared
Field1 format_typeFormat of the field1 like CH, PD, BI etc,.

Example -


Scenario - From the below data, sum the marks at department level.

Input File - MTH.SORT.INPUT

----+----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).

JCL -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
//Job-card
//*                                                                     
//**********************************************************************
//*                                                                     
//* SORT SUM STATEMENT                                                  
//*                                                                     
//**********************************************************************
//STEP01   EXEC PGM=SORT                                                
//SORTIN   DD DSN=MTH.SORT.INPUT01,DISP=SHR                    
//SORTOUT  DD SYSOUT=*                                                  
//SYSOUT   DD SYSOUT=*                                                  
//SYSIN    DD *                                                         
     SORT FIELDS=(30,10,CH,A)                                           
     SUM  FIELDS=(45,03,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 -

  1. 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.
  2. From the requirement,the department is alphabetic. So the type is character (CH).
  3. With the above information gathered, the SORT statement like,
    SORT FIELDS=(30,10,CH,A)
  4. As per requirement, 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.
  5. With all the data gathered, the SUM FIELDS formed like -
    SUM  FIELDS=(45,03,ZD)
  6. 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.