control break based on several keys



IBM's flagship sort product DFSORT for sorting, merging, copying, data manipulation and reporting. Includes ICETOOL and ICEGENER

control break based on several keys

Postby wolfi57 » Thu Dec 22, 2016 1:59 pm

here a post from another site about mainframe , I have the same requirement but the solution doesn't fit 8 my keys are not contigus )
**********************
We have two key fields key1 and key2. Data is sorted first based on the key1 then based on the key 2.

Now my requirement is like below:

a) Whenever key1 value changed from its previous value seqnum resets to 001

b) For every key1 value if the following key 2 value changes the seqnum increments by one.

c) If it doesn’t changed it keeps the original value
Please see below example

INPUT FILE

Key1 key2
234 112
234 112
234 113
234 114
234 114
234 115
235 112
235 112
235 113
235 113
236 117
237 117
238 117

OUTPUT FILE

Key1 key2 SEQNUM
234 112 001
234 112 001
234 113 002
234 114 003
234 114 003
234 115 004
235 112 001
235 112 001
235 113 002
235 113 002
236 117 001
237 117 001
238 117 001

the solution is working only if keys are contigus

//STEP0100 EXEC PGM=SORT                                 
//SYSOUT   DD SYSOUT=*                                   
//SORTIN   DD *                                         
234 112                                                 
234 112                                                 
234 113                                                 
234 114                                                 
234 114                                                 
234 115                                                 
235 112                                                 
235 112                                                 
235 113                                                 
235 113                                                 
236 117                                                 
237 117                                                 
238 117                                                 
//SORTOUT  DD SYSOUT=*                                   
//SYSIN    DD *                                         
  SORT FIELDS=COPY                                       
  INREC IFOUTLEN=80,                                     
  IFTHEN=(WHEN=GROUP,KEYBEGIN=(1,7),PUSH=(81:ID=8)),     
  IFTHEN=(WHEN=GROUP,KEYBEGIN=(1,3),PUSH=(89:81,8)),     
  IFTHEN=(WHEN=INIT,                                     
  OVERLAY=(12:+1,ADD,(81,8,ZD,SUB,89,8,ZD),M11,LENGTH=3))
//*

but when we have something like that
key1 ....... Keys2 .........
how can I have the same result

other question how extend to more than 2 keys
wolfi57
 
Posts: 10
Joined: Fri Dec 16, 2016 10:51 pm
Has thanked: 0 time
Been thanked: 0 time

Re: control break based on several keys

 

Re: control break based on several keys

Postby Aki88 » Thu Dec 22, 2016 2:58 pm

Hello,

Borrowing from your earlier topic and this one, see if below helps; all that has been done here is:
a. Have added data between the two keys, to make them- key1....key2...
b. Have grouped the two keys together using INREC
c. Modified the position of the final OVERLAY
d. Note, I have not removed the new 'contiguous key', that has been built in the modified SORT card for the sole purpose of this test case, you can modify the column-position of the same to something beyond 80 bytes and remove it during the write to output (this has been done to show you how the data looks after the processing of the newly added IFTHEN/INIT), look at columns- 15 to 21

The above returns the same output as you had shown in your post (plus the additional positions); this way we can add as many keys as possible, only thing that changes is the small chunk of code to handle it; final grouping can be done in the same manner or you can choose other logic as well.

Code:

----+----1----+----2----+----3
//SORTIN   DD *              
234 ASD 112                  
234 ASF 112                  
234 FOH 113                  
234 ADS 114                  
234 WEG 114                  
234 GRE 115                  
235 RGA 112                  
235 REG 112                  
235 AER 113                  
235 ARG 113                  
236 AER 117                  
237 HRE 117                  
238 ERA 117                  
/*        
....
....
//SYSIN    DD *                                                        
*                                                                      
* TELL DFSORT THAT THE DATA IS TO BE COPIED, NO REARRANGEMENT OF RECORDS
* IS REQUIRED.                                                          
*                                                                      
  SORT FIELDS=COPY                                                      
*                                                                      
* FIT THE OUTPUT INTO 80 BYTE RECORD POST PROCESSING ON IFTHEN STATEMENT
*                                                                      
  INREC IFOUTLEN=80,                                                    
*                                                                      
* EXTEND THE RECORD READ, PAD THE INITIAL KEYS TO A DIFFERENT COLUMN,  
* THIS NEW KEY IS THE ONE ON WHICH THE PROCESS WILL BE DRIVEN NOW.      
*                                                                      
  IFTHEN=(WHEN=INIT,                                             --> New code       
          OVERLAY=(15:1,3,19:9,3)),                            --> New code        
*                                                                      
* GROUP THE RECORDS ON THE BASIS OF THE COMPLETE NEW KEY, AND ASSIGN AN
* IDENTIFIER NUMBER OF 8 BYTES.                                        
*                                                                      
  IFTHEN=(WHEN=GROUP,KEYBEGIN=(15,7),PUSH=(81:ID=8)),                  
*                                                                      
* NOW GROUP THE SAME RECORD ON THE BASIS OF FIRST 3 BYTES OF THE SAME  
* KEY, AND APPEND THE EARLIER ADDED 8 BYTES ID HERE. THIS PADS THE GROUP
* ID OF THE FIRST RECORD OF THE (1,3) GROUP FOR EACH UNIQUE GROUP.      
*                                                                      
  IFTHEN=(WHEN=GROUP,KEYBEGIN=(15,3),PUSH=(89:81,8)),                  
*                                                                      
* AT THIS POINT, THE RECORDS HAVE UNIQUE IDENTIFIERS ASSIGNED TO THEM  
* THAT GROUP THEM IN THE MANNER THE USER WANTED.                        
* NOW, FORMATTING THE DATA AND ASSIGNING A SINGLE FORMATTED IDENTIFIER  
* TO EACH RECORD OF THE GROUP; THIS IS DONE BY SUBTRACTING THE 3 BYTE  
* GROUPS ID FROM THE MASTER 7 BYTE GROUPS ID.                          
*                                                                      
* NOTE: ONE OF THE TWO IS A SUBSET OF THE OTHER, YET AN IMPORTANT POINT
*       HERE IS, AS YOU'VE ALREADY NOTICED, EITHER OF THEM ARE          
* INDIVIDUALLY TWO DIFFERENT GROUPS.                                    
*                                                                      
  IFTHEN=(WHEN=INIT,                                                    
  OVERLAY=(23:+1,ADD,(81,8,ZD,SUB,89,8,ZD),M11,LENGTH=3))      
/*                                                              
 


SORTOUT:

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
*             <-----> <->             --> COMMENT LINES                
*            MODIFIED GRP             --> WHICH ARE ADDED              
*              KEY    ID              --> FOR EXPLANATION OF OUTPUT    
234 ASD 112   234 112 001                                              
234 ASF 112   234 112 001                                              
234 FOH 113   234 113 002                                              
234 ADS 114   234 114 003                                              
234 WEG 114   234 114 003                                              
234 GRE 115   234 115 004                                              
235 RGA 112   235 112 001                                              
235 REG 112   235 112 001                                              
235 AER 113   235 113 002                                              
235 ARG 113   235 113 002                                              
236 AER 117   236 117 001                                              
237 HRE 117   237 117 001                                              
238 ERA 117   238 117 001                                              
 
Aki88
 
Posts: 371
Joined: Tue Jan 28, 2014 1:52 pm
Has thanked: 33 times
Been thanked: 35 times

Re: control break based on several keys

Postby wolfi57 » Thu Dec 22, 2016 5:32 pm

thank you it works ,
I thought that keybegin only refer to original fields and not applied to overlay
really thank you , now with your solution on other post , I can manage my selection :)
wolfi57
 
Posts: 10
Joined: Fri Dec 16, 2016 10:51 pm
Has thanked: 0 time
Been thanked: 0 time


Return to DFSORT/ICETOOL/ICEGENER

 


  • Related topics
    Replies
    Views
    Last post