Page 1 of 1

Sort only detail records

PostPosted: Fri Nov 04, 2016 9:25 am
by Papya013
Hi Team,

I have an input file as mentioned below

HD HEADER 1234567
LN TRAILER 1111
LN TRAILER 3333
LN TRAILER 5555
LN TRAILER 4444
LN TRAILER 2222
HD HEADER 2345678
LN TRAILER 2111
LN TRAILER 2333
LN TRAILER 2555
LN TRAILER 2444
LN TRAILER 2221


I would need the output as mentioned below.

HD HEADER 1234567
LN TRAILER 1111
LN TRAILER 2222
LN TRAILER 3333
LN TRAILER 4444
LN TRAILER 5555
HD HEADER 2345678
LN TRAILER 2111
LN TRAILER 2221
LN TRAILER 2333
LN TRAILER 2444
LN TRAILER 2555


I used below mentioned SORT card but its working if there is only one header but my input file can contain more than one header.

INREC IFTHEN=(WHEN=GROUP,BEGIN=(1,2,CH,EQ,C'LN'),PUSH=(168:12,4))
SORT FIELDS=(168,4,CH,A),EQUALS        


Could anyone please assign me with this?

Re: Sort only detail records

PostPosted: Fri Nov 04, 2016 11:43 am
by Aki88
Hello,

With your current SORT card, after the 'INREC IFTHEN' operation, the data changes to (I've rearranged the OVERLAY column from 168 to 60):


HD HEADER 1234567                                              
LN TRAILER 1111                                            1111
LN TRAILER 3333                                            3333
LN TRAILER 5555                                            5555
LN TRAILER 4444                                            4444
LN TRAILER 2222                                            2222
HD HEADER 2345678                                          2222
LN TRAILER 2111                                            2111
LN TRAILER 2333                                            2333
LN TRAILER 2555                                            2555
LN TRAILER 2444                                            2444
LN TRAILER 2221                                            2221
 


Since you do not have an identifier to GROUP your HEADER record, the second header is actually treated a part of the GROUP - LN, which is immediately before the second HEADER record. Which is why you have '2222' instead of 4 bytes of space when the second HEADER is encountered.


LN TRAILER 4444                                            4444
LN TRAILER 2222                                            2222 ---> Clean OVERLAY
HD HEADER 2345678                                          2222  --> Since GROUP remains same, hence OVERLAYd value
LN TRAILER 2111                                            2111
 


Now when you perform a SORT on the OVERLAY'd 4 bytes of data, with EQUALS, it changes to (EQUALS simply shuffles the order of the two '2222' group records):


HD HEADER 1234567                                              
LN TRAILER 1111                                            1111
LN TRAILER 2111                                            2111
LN TRAILER 2221                                            2221
LN TRAILER 2222                                            2222
HD HEADER 2345678                                          2222
LN TRAILER 2333                                            2333
LN TRAILER 2444                                            2444
LN TRAILER 2555                                            2555
LN TRAILER 3333                                            3333
LN TRAILER 4444                                            4444
LN TRAILER 5555                                            5555 


One of the multiple possible solutions would be GROUPing on the basis of HEADER, and padding the detail records individually:


*                                                                      
* GROUP THE DATA ON THE BASIS OF HEADER RECORD, AND PAD THE HEADER VALUE
* TO EACH RECORD OF THIS GROUP; THIS HELPS IN UNIQUELY IDENTIFYING THE  
* RECORDS AT THE TIME OF SORTING.                                      
*                                                                      
 INREC IFTHEN=(WHEN=GROUP,BEGIN=(1,2,CH,EQ,C'HD'),PUSH=(81:11,7)),      
*                                                                      
* PAD THE DETAIL RECORDS WITH 4 BYTES OF IDENTIFIER FOR EACH RECORD.    
*                                                                      
       IFTHEN=(WHEN=(1,2,CH,EQ,C'LN'),OVERLAY=(88:12,4))                
*                                                                      
* SORT THE DATA ON THE BASIS OF EXTENDED RECORD'S VALUE.                
*                                                                      
 SORT FIELDS=(81,11,CH,A)                                              
*                                                                      
* REMOVE THE PADDED DATA.                                              
*                                                                      
 OUTFIL REMOVECC,BUILD=(1,80)                                          
 


Gives:


HD HEADER 1234567  
LN TRAILER 1111    
LN TRAILER 2222    
LN TRAILER 3333    
LN TRAILER 4444    
LN TRAILER 5555    
HD HEADER 2345678  
LN TRAILER 2111    
LN TRAILER 2221    
LN TRAILER 2333    
LN TRAILER 2444    
LN TRAILER 2555    
 

Re: Sort only detail records

PostPosted: Fri Nov 04, 2016 11:56 am
by Papya013
Thanks a lot for your quick response. It worked:)

Re: Sort only detail records

PostPosted: Fri Nov 04, 2016 12:09 pm
by BillyBoyo
Identify the header and PUSH from that. If the header values are ascending order, use that, if not, use the ID.

From the detail records, temporarily extend so that you can have the key you want to sort on in a position which doesn't clash with any data on the header. Use IFOUTLEN for the maximum lengths. Sort with the two sort keys, that of the header first, followed by what you want to sequence on within the header (which you'lll find will be space on the header). Use BUILD on OUTREC to drop off the extended data.

Turns out Aki88 was doing about the same thing at the same time :-)

Pay attention to the IFOUTLEN and the ID if the headers are not in sequence.

And by now there's even a response...

Re: Sort only detail records

PostPosted: Fri Nov 04, 2016 3:14 pm
by Papya013
Hi,

I have modified the code as below.

INREC IFTHEN=(WHEN=INIT,OVERLAY=(184:1,2)),                
IFTHEN=(WHEN=GROUP,BEGIN=(1,2,CH,EQ,C'HD'),PUSH=(176:ID=8)),
IFTHEN=(WHEN=(1,2,CH,EQ,C'LN'),OVERLAY=(186:9,7))          
SORT FIELDS=(176,17,CH,A),EQUALS                            
OUTREC BUILD=(1,175)