Monday, April 10, 2017

Using JCL Sort to create a report

Reports are an integral part of the process. In mainframes we use cobol to create reports usually. However SORT is a powerful tool to create a report if you need one pretty quick. 

The below example is for a report creating sort with headers, summary and trailer.

The Input file has Header (HDR) and Detail (DTL) lines. 
The header has success record count on position 29,6 and failure record count on 35,06
The detail record has data with status code and description

First of all we sort the data so that HDR record comes up last and DTL records come up first. This is because the count information is gathered from the header and shown at the end of the report. 

/ is used in the build statements to initiate a new Line. 2/ gives us two new lines
COUNT=(M11,LENGTH=6)gives the total number of records. 


//STEP0001 EXEC PGM=SORT                                                
//*                                                                     
//SORTIN   DD *                                                         
HDR SAMPLERPT04042017120322 000005000002                                
DTL DATA-1 NAME-1 ADDR-1                  0000 SUCCESS                  
DTL DATA-2 NAME-2 ADDR-2                  0000 SUCCESS                  
DTL DATA-3 NAME-3 ADDR-3                  0000 SUCCESS                  
DTL DATA-4 NAME-4 ADDR-4                  0000 SUCCESS                  
DTL DATA-5 NAME-5 ADDR-5                  0000 SUCCESS                  
DTL DATA-6 NAME-6 ADDR-6                  0020 NAME ERROR               
DTL DATA-7 NAME-7 ADDR-7                  0010 ID ERROR                 
/*                                                                      
//*                                                                     
//SORTOUT  DD SYSOUT=*                                                  
//*                                                                     
//SYSOUT   DD SYSOUT=*                                                  
//SYSPRINT DD SYSOUT=*                                                  
//*                                                                     
//SYSIN    DD *                                                         
   SORT FIELDS=(01,02,CH,A)                                              
   OUTFIL REMOVECC,                                                      
          IFTHEN=(WHEN=(01,02,CH,EQ,C'DTL'),                             
                  BUILD=(05,07,X,12,07,X,19,15,X,43,04,X,48,20)),        
          IFTHEN=(WHEN=(01,02,CH,EQ,C'HDR'),                             
                  BUILD=(/,C'TOTAL NUMBER OF SUCCESS RECORDS : ',29,06,  
                         /,C'TOTAL NUMBER OF ERROR RECORDS   : ',35,06,  
                         /,C'TOTAL NUMBER OF DETAIL RECORDS  : ',        
                           (29,06,ZD,ADD,35,06,ZD),EDIT=(TTTTTT))),      
   HEADER2=(1:C'REPORT ID  : XXXXXX',                                    
            26:C' YOUR COMPANY NAME HERE  ',69:C'PAGE: ',75:PAGE,/,      
            1:C'REPORT DATE: ',                                          
            14:DATE=(MD4-),27:C' YOUR REPORT NAME HERE ',                
            2/,                                                          
            C'ID',6X,                                                    
            C'NAME',4X,                                                  
            C'ADDRESS',9X,                                               
            C'ERR ',X,                                                   
            C'ERR-DESC',                                                 
            /,                                                   
            C'-------',X,                                        
            C'-------',X,                                        
            C'---------------',X,                                
            C'----',X,                                           
            C'------------------------------'),                  
   TRAILER1=(C'TOTAL NUMBER OF RECORDS         : ',              
             COUNT=(M11,LENGTH=6),                               
             /,29C'*',C' E N D O F R E P O R T ',28C'*')         

/*                                                              

The Output is as follows

REPORT ID  : XXXXXX       YOUR COMPANY NAME HERE                    PAGE:      1
REPORT DATE: 04-10-2017    YOUR REPORT NAME HERE                                
                                                                                
ID      NAME    ADDRESS         ERR  ERR-DESC                                   
------- ------- --------------- ---- ------------------------------             
DATA-1  NAME-1  ADDR-1          0000 SUCCESS                                    
DATA-2  NAME-2  ADDR-2          0000 SUCCESS                                    
DATA-3  NAME-3  ADDR-3          0000 SUCCESS                                    
DATA-4  NAME-4  ADDR-4          0000 SUCCESS                                    
DATA-5  NAME-5  ADDR-5          0000 SUCCESS                                    
DATA-6  NAME-6  ADDR-6          0020 NAME ERROR                                 
DATA-7  NAME-7  ADDR-7          0010 ID ERROR                                   
                                                                                
TOTAL NUMBER OF SUCCESS RECORDS : 000005                                        
TOTAL NUMBER OF ERROR RECORDS   : 000002                                        
TOTAL NUMBER OF DETAIL RECORDS  : 000007                                        
TOTAL NUMBER OF RECORDS         : 000008                                      
***************************** E N D O F R E P O R T ****************************