Summing Fields

Sort can be used to sum fields using SORT and SUM statements. Let's take the below example of a list of Students appearing for different Subjects in Mechanical Engineering. The Maximum Score for each Subject and the Score secured by each Student in the Subject is provided.














If we have a requirement to get the Total Score for each Student and the Maximum Score possible for each, SUM statement will be useful.

















The below code is present in the above screenshot.

//SORTJCL# JOB (NXYZ),'SORT',NOTIFY=&SYSUID,CLASS=0,MSGCLASS=5
//***************************************************************
//* SORT JOB TO SUM ON TWO FIELDS FOR SORT ON ANOTHER FIELD  
//***************************************************************
//JS0100   EXEC PGM=SORT
//   PARM='DYNALLOC=(SYSDA,255),VSCORE=2M,VSCORET=256M'
//SORTIN   DD DISP=SHR,DSN=Input File Name,
//SORTOUT  DD DSN=Output File Name,
//         DISP=(NEW,CATLG,DELETE),
//         SPACE=(CYLS,(100,40),RLSE),                              
//         DATACLAS=DCTCOM,VOL=(,,,24)
//SYSOUT   DD SYSOUT=*
//SYSIN    DD *
 SORT FIELDS=(1,7,CH,A)
 SUM FIELDS=(35,3,ZD,53,3,ZD)                       
/*

The final output will have the Sum Total of Marks for all Subjects for each Student as well as the Total Marks Secured by the Student across all Subjects.








For the Subject field, the first row will be retained. It maybe more prudent to remove this field from the final result using an OUTREC statement.

No comments: