I usually avoid lag function because at times the result could be unexpected. If the case is as simple as you've stated then for this case you can probably use the lag function.Code:data streak(drop=last); set streak; retain last streak; if _N_=1 then do; if result=1 then streak=1; else streak=-1; end; else do; if result=1 and last=1 then streak=streak+1; else if result=1 and last=0 then streak=1; else if result=0 and last=1 then streak=-1; else streak=streak-1; end; last=result; run; proc print data=streak; run;





Reply With Quote