Thanks Tom. That gave me required clue. I modified the code to ignore 0 and missing in the dif variable. data want;
*retain id group dif count;
set have (drop= sum);
by id;
if first.id then gr=1;
else if (dif > 2 & dif ne .) then gr+1;
count=1;
if dif = . or dif = 0 then count=.;
run;
proc sql;
create table want2 as
select *, SUM( count ) as dur
from want
group by id,gr
;
quit; But one thing I noticed that in the want2 dataset it is changing the order of observations in the dif variable. Number 3 is appearing on the last row in dif. Is there any way to maintain the order as in the want dataset?
... View more