Saturday, March 14, 2015

JCL: Empty Dataset Check

Introduction

There are times where you want to execute some steps in JCL, if a given input dataset is NOT empty and execute some other steps if the dataset is EMPTY. For example, we run a given JCL several times a day that appends comma separated records into a given dataset and we need to include the header only during the first time.

How do we do it ?

For example, Lets say a job that runs several time a day and append some records to a dataset called &HLQ..S00.ACCTINFO.D&YYMMDD..CSV where the job has to add header only when the job runs for the first time and subsequent runs just the records alone.

Option 1 : Using ICETOOL

We can use the ICETOOL utility that comes as a part of SORT to check whether a file is empty. It can be done using the COUNT statement. See example below.


  //*******************************************************        
  //*   Check whether the file is empty                           
  //*   HLQ=XYZ, 
  //*   LMMDDYY=150314                           
  //*******************************************************        
  //S0000002 EXEC PGM=ICETOOL                                      
  //IN       DD DSN=&HLQ..S00.ACCTINFO.D&LYYMMDD..CSV,             
  //         DISP=SHR                                              
  //TOOLMSG  DD SYSOUT=*                                           
  //DFSMSG   DD SYSOUT=*                                           
  //TOOLIN   DD DATA                                               
  COUNT FROM(IN) EMPTY                                             
  /*                                                               
Here ICETOOL returns a NON ZERO RETURN CODE, if the file is empty, so the step to include the header can be run using COND parameter or IF statement

Option 2 : Using IDCAMS Utility

We can also use the IDCAMS utility to check whether a file is empty. It can be done using the REPRO command with COUNT(1) parameter. See example below.

There is a subtle difference between using IDCAMS and ICETOOL utilities if the file is not empty. IDCAMS writes the first record into the OUTFILE(OUT) DD name, if the file is NOT empty. Suppose if the file contents are too sensitive that you don't want that dumped into a SYSOUT, the OUT DD can be coded as a temporary dataset with DISP=(NEW,DELETE,DELETE) so that the contents are not visible to anyone after the job has completed.


  //*******************************************************        
  //*   Check whether the file is empty                           
  //*   HLQ=XYZ, 
  //*   LMMDDYY=150314                           
  //*******************************************************        
  //S0000002 EXEC PGM=IDCAMS                               
  //IN       DD DSN=&HLQ..S00.ACCTINFO.D&LYYMMDD..CSV,     
  //         DISP=SHR                                      
  //OUT      DD SYSOUT=*                                   
  //SYSPRINT DD SYSOUT=*                                   
  //SYSIN    DD DATA                                       
    REPRO INFILE(IN) OUTFILE(OUT) COUNT(1)                  
  /*                                                       
Here IDCAMS returns a NON ZERO RETURN CODE, if the file is empty, so the step to include the header can be run using COND parameter or IF statement

Example JCL

Here is a complete example using the ICETOOL to accomplish what was discussed in the "Introduction" section. This JOB contains 4 steps.

  1. Allocate a Dataset if it doesn't exists using IEFBR14.
  2. Empty dataset check using ICETOOL, if empty returns a non-zero Return Code.
  3. Conditionally execute IEFBR14 to include header if previous step's RC is greater than zero.
  4. Step to append records to dataset. I have used IEBGENER, it can be a user written or another built-in program


  //*** YOUR JOB CARD HERE **
  //*
  //    SET HLQ=JUNI                                     
  //    SET LYYMMDD=150314                                 
  //*******************************************************
  //* Allocate Dataset if it doesnt exist                 *
  //*******************************************************
  //S0000001 EXEC PGM=IEFBR14                              
  //DD001    DD DSN=&HLQ..S00.ACCTINFO.D&LYYMMDD..CSV,     
  //         DISP=(MOD,CATLG,DELETE),                      
  //         DCB=(LRECL=80,RECFM=FB),                      
  //         SPACE=(TRK,(1),RLSE)                          
  //*                                                      
  //*******************************************************
  //*   Check whether the file is empty                   *
  //*******************************************************
  //S0000002 EXEC PGM=ICETOOL                              
  //IN       DD DSN=&HLQ..S00.ACCTINFO.D&LYYMMDD..CSV,     
  //         DISP=SHR                                      
  //TOOLMSG  DD SYSOUT=*                                   
  //DFSMSG   DD SYSOUT=*                                   
  //TOOLIN   DD DATA                                       
  COUNT FROM(IN) EMPTY
  /*                           
  //  IF (S0000002.RC > 0) THEN       
  //******************************************************* 
  //* Add Header to Empty File                            * 
  //*******************************************************  
  //S0000003 EXEC PGM=IEBGENER           
  //SYSUT1   DD   *                     
  AcctNbr,LastName,FirstName,MI,DOB,AcctType,Balance,End 
  /*              
  //SYSUT2   DD DSN=&HLQ..S00.ACCTINFO.D&LYYMMDD..CSV,   
  //         DISP=OLD                                     
  //SYSPRINT DD SYSOUT=*                                   
  //SYSIN    DD DUMMY                                    
  //*
  //  ENDIF                                                
  //*******************************************************
  //* Append detailed records                             *
  //*******************************************************
  //S0000004 EXEC PGM=IEBGENER                             
  //SYSUT1   DD   *                                        
  987262601,Doe,John,J,05/22/1997,Savings,12500.00,Z       
  987262602,Smith,John,K,04/14/1968,Current,-100.00,Z      
  /*                                                       
  //SYSUT2   DD DSN=&HLQ..S00.ACCTINFO.D&LYYMMDD..CSV,     
  //         DISP=(MOD,CATLG,DELETE),                      
  //         DCB=(LRECL=80,RECFM=FB),                      
  //         SPACE=(TRK,(1),RLSE)                          
  //SYSPRINT DD SYSOUT=*                                   
  //SYSIN    DD DUMMY                                      
  //*                                                      
  //*******************************************************
  //*               End of JOB                            *
  //*******************************************************

No comments:

Post a Comment