Thank you, @AhmedAl_Attar . It's very close to what I want, but it breaks down for the first row of this new example. FLAG1=10 when it should equal 4 to reflect the 4 months of continuous enrollment. Do you happen to know how to fix this?
data have;
input person $ month_01 month_02 month_03 month_04 month_05 month_06 month_07 month_08 month_09 month_10;
cards;
A 0 0 0 0 0 0 1 1 1 1
B 1 0 1 1 1 1 0 1 1 1
C 0 0 1 1 1 1 0 0 0 0
D 0 0 0 0 0 1 0 0 0 0
E 0 1 1 1 0 1 1 1 0 0
;
run;
data want(KEEP=mon: fl: comp:) ;
set have;
array m {*} 3 mon:;
length str $10;
call missing (complete1,complete2,flag1,flag2);
str = cats(of m{*});
put str=;
start=1;
stop=10;
cptrn= prxparse('/1{3}/');
fptrn= prxparse('/1{3,}0/');
/* Find first 3 months of continuous enrollment */
pos=prxmatch(cptrn,str);
if (pos > 0) then
do;
complete1=pos+2; /* Record the position at the of pattern */
start=complete1; /* Change our starting position */
end;
/* Find Total months of enrollment for the time period corresponding to COMPLETE1 */
pos2=prxmatch(fptrn,str);
if ( pos2 > 0) then
flag1=findc(str,'0',pos2) - pos2; /* Count number of months */
/* Find second/additional 3 months of continuous enrollment */
if (flag1 > 0) then
do;
call prxnext(cptrn,start,stop,str,position,length);
do while(position>0);
complete2=position+2;
pos2=findc(str,'0',position);
if ( pos2 > 0) then
flag2 = (pos2 - position); /* Count number of months */
else
flag2 = (10 - position + 1);
call prxnext(cptrn,start,stop,str,position,length);
end;
end;
if ((complete1 > 0) and (flag1 <=0)) then
flag1 = 10;
run;
proc print data=want (obs=1); run;
... View more