Hello, I need to count the months of continuous enrollment per member. When there are gaps in enrollment the count must reset for the member.
Here is the data format:
memberid enrollment
111 202201
111 202202
111 202205
222 202205
222 202206
Here's the output I'm looking for:
memberid enrollment cont_enroll_count
111 202201 2
111 202202 2
111 202205 1
222 202205 2
222 202206 2
Also, if there's another way to do this, please let me know!
Is enrollment an actual SAS Date value or simply the integer as we see it: 202201 ?
the enrollment date is in numeric format
What format is applied to it?
Then you may have to change the date values into Actual SAS Dates for the posted code to work.
Try this
data have;
input memberid enrollment : yymmn6.;
format enrollment yymmn6.;
datalines;
111 202201
111 202202
111 202205
222 202205
222 202206
;
data temp;
set have;
by memberid;
lag = ifn(first.memberid, ., lag(enrollment));
if first.memberid | intck('month', lag, enrollment) > 1 then c = 1;
else c + 1;
if c = 1 then g + 1;
run;
proc sql;
create table want as
select memberid
, enrollment
, max(c) as cont_enroll_count
from temp
group by g
;
quit;
Result:
memberid enrollment cont_enroll_count 111 202201 2 111 202202 2 111 202205 1 222 202206 2 222 202205 2
That is why you have to change the date values into Actual SAS Dates. Then you will not have the problem.
Another possibility is to just code for the year ends. If you have data like this:
data have;
input memberid enrollment;
datalines;
111 202201
111 202202
111 202205
222 202205
222 202206
333 202112
333 202201
;run;
You can first create the groups of continued enrollment like this (89 is just the difference between e.g. 202201 and 202112, the end of the year):
data groups;
set have;
by memberid;
dif=dif(enrollment);
if first.memberid then
group=1;
else if dif not in(1,89) then group+1;
drop dif;
run;
And then use that to get the counts:
data want;
do cont_enroll_count=1 by 1 until(last.group);
set groups;
by memberid group;
end;
do until(last.group);
set groups;
by memberid group;
output;
end;
run;
Of course, with a date format solution (as suggested by @PeterClemmensen) you also check if the months entered are valid. If they are not, the results from the method shown here may get "interesting" (e.g. if you have 202113 followed by 202202, it will just count as continued enrollment, rather than giving an error).
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.