Friday, March 19, 2021

Solving a problem statement using IBM DFSORT

Folks, in this post let's try to solve a problem - which I recently came across - using IBM's DFSORT utility. 



Problem statement

There are 2 flags for an account number. 
If both the flags are 'Y', the output dataset should contain two records with account number and the name of the flag. 
If any one of the flag is 'Y', output dataset should contain only one record with account number and the name of the flag. 

For example, Account AAA has got two flags set to 'Y', so the output dataset should contain two records for the account AAA.

Input:



Output:



My approach


As soon as I realized that a record in the input must be broken into 2 when both the flags of an account number are 'Y', I recalled the usage of / or n/ which is used to insert blank records in the output. But,  / or n/ is supported only by the OUTFIL control statement. 

So, OUTFIL control statement with IFTHEN...WHEN condition can be used to validate the flags and write the account number and the name of the flag to the output dataset. 

As the problem statement implicitly states that an account number with both the flags as 'N' be omitted, OMIT COND can be used to exclude such records before sorting.

Alright! Let's code the control statements. 

 =COLS> ----+----1----+----2----+----3----+----4----+----5----+----6----+----7--  
 ****** ***************************** Top of Data ******************************  
 000001 //Z01071A JOB 1,NOTIFY=&SYSUID                       
 000002 //STEP01 EXEC PGM=SORT                           
 000003 //SORTIN DD *                               
 000004 AAA Y Y                                   
 000005 BBB Y N                                   
 000006 CCC N Y                                   
 000007 DDD N N                                   
 000008 //SORTOUT DD SYSOUT=*                            
 000009 //SYSOUT DD SYSOUT=*                            
 000010 //SYSIN  DD *                               
 000011  SORT FIELDS=COPY                             
 000012  OMIT COND=(5,1,CH,EQ,C'N',AND,7,1,CH,EQ,C'N')               
 000013  OUTFIL IFTHEN=(WHEN=(5,1,CH,EQ,C'Y',AND,7,1,CH,EQ,C'Y'),         
 000014     BUILD=(1,3,X,C'KA',/,1,3,X,C'TN')),                
 000015     IFTHEN=(WHEN=(5,1,CH,EQ,C'Y',AND,7,1,CH,EQ,C'N'),         
 000016     BUILD=(1,3,X,C'KA')),                       
 000017     IFTHEN=(WHEN=(5,1,CH,EQ,C'N',AND,7,1,CH,EQ,C'Y'),         
 000018     BUILD=(1,3,X,C'TN'))                        
 ****** **************************** Bottom of Data ****************************  
There are 3 conditions coded in the OUTFIL IFTHEN control statement. 
  • The first condition (in line #13) checks for both the flags to be 'Y'. If true, 2 records should be written in the output dataset with the account number and the name of the flag in each record. Note the BUILD parameter (in line #14) with a / to indicate a new output record to be started after writing the account number with the first flag name. The account number and the second flag's name will be written in the new output record. 
  • The second and third conditions writes to the output dataset, the account number and the flag name, if any of the flag is 'Y'.

Output after submitting the JCL:
 ********************************* TOP OF DATA **********************************  
 AAA KA                                       
 AAA TN                                       
 BBB KA                                       
 CCC TN                                       
 ******************************** BOTTOM OF DATA ********************************  
There you go!

Now, it's your turn. Use the comments section to show how your approach would've been (or will be) for this problem statement.


1 comment: