@blackandwhite:
Looks like a backtracking task. So, one approach is to read every group of records ending in Y=1 twice, initializing Z=0 at the beginning of each ID by-group and incrementing Z before each second pass:
data have ;
input ID ID_sub :$1. Y ;
cards ;
1 A 0
1 B 0
1 C 1
2 A 1
2 B 0
2 C 0
2 D 1
3 A 0
3 B 0
3 C 0
3 D 0
3 E 0
3 F 0
3 G 0
4 A 0
4 B 1
4 C 1
5 A 1
5 B 1
run ;
data want ;
do until (Y) ;
set have ;
by id ;
if first.id then Z = 0 ;
end ;
Z + 1 ;
do until (Y) ;
set have ;
output ;
end ;
run ;
Kind regards
Paul D.
... View more