data have;
input id $ group;
cards;
a 1
a 1
a .
a 1
a 1
b 1
b .
b 1
b 1
b .
b 1
c .
c .
c 1
c .
c .
c 1
c 1
;
data wanted;
set have;
by id;
retain missflag temp;
if first.id then temp=^missing(group);
if missing(group) then missflag=1;
else do;
if missflag=1 then do;
temp+1;
missflag=0;
end;
new_grou ...
data have;
input id $ group;
cards;
a 1
a 1
a .
a 1
a 1
b 1
b .
b 1
b 1
b .
b 1
c .
c .
c 1
c .
c .
c 1
c 1
;
data wanted;
set have;
by id;
retain missflag temp;
if first.id then temp=^missing(group);
if missing(group) then missflag=1;
else do;
if missflag=1 then do;
temp+1;
missflag=0;
end;
new_group=temp;
end;
drop missflag temp;
run;
data test2;
set test1;
id2=lag(id);
group2=lag(group);
retain new_group;
if id^=id2 then do;
if group=. then new_group=0;
else new_group=1;
end;
else if group=1 and group2=. then new_group=new_group+1;
run;
data test3;
set test2;
if group=. then new_group=.;
run;