Author: Bill Woodger
Subject: Reply to: Select first 2 group data
Posted: Tue Jun 14, 2016 7:45 am (GMT 5.5)
Rohit,
You've over-engineered yours by trying to get a 1/2 answer, when LT 2 would do. With WHEN=GROUP you'd need to do that subtraction, so it basically does work.
magesh23586's solution works like this.
The first RESTART will get each entire key numbered from 1-N. Of these, 2-N are unimportant, it is the "1" marking the start of the groups which is important.
The second RESTART only operates on the "1"s, so the first "1" will get "1", the second "2", the others are then irrelevant.
Having only marked the first record of each group with the second RESTART, the WHEN=GROUP on OUTREC propagates that to all the records in the group, then the INCLUDE= can pick all the records marked with 1s or 2s.
The sequence numbers need to be large enough to cover the number of records within a group, else mayhem will, as usual, ensue.
If a SORT were required as well, things could be different (due to the use of OUTREC because WHEN=GROUP cannot follow WHEN=(logicalexpression)). Depends on the data and the actual requirement for that case.
Here's an alternate. Since the records which aren't wanted aren't wanted, you can destroy them (or a byte of them) to indicate they are not wanted.
This can also be applied to Rohit's solution which works even if a SORT is needed.
Subject: Reply to: Select first 2 group data
Posted: Tue Jun 14, 2016 7:45 am (GMT 5.5)
Rohit,
You've over-engineered yours by trying to get a 1/2 answer, when LT 2 would do. With WHEN=GROUP you'd need to do that subtraction, so it basically does work.
magesh23586's solution works like this.
The first RESTART will get each entire key numbered from 1-N. Of these, 2-N are unimportant, it is the "1" marking the start of the groups which is important.
The second RESTART only operates on the "1"s, so the first "1" will get "1", the second "2", the others are then irrelevant.
Having only marked the first record of each group with the second RESTART, the WHEN=GROUP on OUTREC propagates that to all the records in the group, then the INCLUDE= can pick all the records marked with 1s or 2s.
The sequence numbers need to be large enough to cover the number of records within a group, else mayhem will, as usual, ensue.
If a SORT were required as well, things could be different (due to the use of OUTREC because WHEN=GROUP cannot follow WHEN=(logicalexpression)). Depends on the data and the actual requirement for that case.
Here's an alternate. Since the records which aren't wanted aren't wanted, you can destroy them (or a byte of them) to indicate they are not wanted.
This can also be applied to Rohit's solution which works even if a SORT is needed.
Code: |
OPTION COPY INREC IFTHEN=(WHEN=INIT, OVERLAY=(81:SEQNUM,2,ZD,RESTART=(1,10))), IFTHEN=(WHEN=(81,2,CH,EQ,C'01'), OVERLAY=(83:SEQNUM,2,ZD,RESTART=(1,5))) OUTREC IFTHEN=(WHEN=GROUP,BEGIN=(83,2,CH,NE,C' '), PUSH=(85:83,2)), IFTHEN=(WHEN=(85,2,CH,GT,C'02'), OVERLAY=(1:C' ')), IFOUTLEN=16 OUTFIL OMIT=(1,1,CH,EQ,C' ') |