please try the below code
data TEST;
infile datalines dsd truncover;
input id:BEST12. score:BEST12. seq:BEST12. vis:BEST12.;
datalines4;
1,22,1,1
1,23,2,1
2,12,1,1
2,22,2,1
3,11,1,1
3,23,2,1
3,19,3,2
3,13,4,2
;;;;
proc sort data=test;
by id vis;
run;
data want;
do until(last.id);
set test;
by id vis;
output;
end;
do until(last.id);
set test;
by id vis;
lags=lag(score);
if last.vis then do;
score=score-lags;
seq=seq+1;
output;
end;
end;
drop lags;
run;
proc sort data=want;
by id vis;
run;
data want2;
set want(drop=seq);
by id vis ;
retain seq;
if first.id then seq=1;
else seq+1;
run;
proc sort data=want2;
by id vis seq;
run;
... View more