proc sql;
create table num_month as
select distinct name, year(month) as year, count(*) as n
from a
group by 1,2
order by 1,2;
quit;
data cal_allgrp;
set num_month;
by name year;
lagyear=lag(year);
if name ne lag(name) then do;
group="C";
num=0;
end;
else do;
if year=lagyear+1 then group="B";
if n=12 then num+1;
if num>1 and group="B" then group="A";
end;
keep name group;
run;
proc sort data=cal_allgrp;
where group ne '';
by name group;
run;
proc sort data=cal_allgrp out=wanted nodupkey;
by name;
run;
you are correct, sorry for my carelessness, please try this one and take a look at if there's any problem, thanks.
proc sort data=a;
by name month;
run;
data consec_month;
set a;
by name month;
mon=month(month);
year=year(month);
lagmon=lag(mon);
lagyear=lag(year);
if first.name then call missing(lagmon,lagyear);
if year=lagyear then num=mon-lagmon;
if year=lagyear+1 and mon=1 and lagmon=12 then num=1;
run;
data getgrp;
set consec_month;
by name month;
if first.name then consnum=0;
if num=1 then consnum+1;
else consnum=0;
group="C";
if year=lag(year)+1 then group="B";
if consnum>=23 then group="A";
keep name group;
run;
proc sort data=getgrp;
by name group;
run;
proc sort data=getgrp out=wanted nodupkey;
by name;
run;