<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Identifying length of enrollment in a program from indicator variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872387#M344669</link>
    <description>&lt;P&gt;What are you going to do when you have more than 10 months of data? A solution that works for this data set might have to be significantly rewritten for 15.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't clearly define which "month" value you want. It appears to be the LAST of a three month period is that correct?&lt;/P&gt;</description>
    <pubDate>Wed, 26 Apr 2023 20:58:47 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-04-26T20:58:47Z</dc:date>
    <item>
      <title>Identifying length of enrollment in a program from indicator variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872367#M344659</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I have a dataset with indicators for if a given person was enrolled in a program at various months. I want to create the following variables:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;COMPLETE1 = Month that marks first 3 months of continuous enrollment&lt;/P&gt;
&lt;P&gt;COMPLETE2 = If there are multiple periods of continuous enrollment, the month that marks the first 3 months of continuous enrollment for the next period&lt;/P&gt;
&lt;P&gt;FLAGS1 = Total months of enrollment for the time period corresponding to COMPLETE1&lt;/P&gt;
&lt;P&gt;FLAGS2 = Total months of enrollment for the time period corresponding to COMPLETE2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was able to obtain COMPLETE1 but not the others. Any help would be appreciated.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 1 1 1 1 1 1 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;
 input person $ month_01 month_02 month_03 month_04 month_05 month_06 month_07 month_08 month_09 month_10 complete1 complete2 flags1 flags2;
 cards;
A 1 1 1 1 1 1 1 1 1 1 3 . 10 .
B 1 0 1 1 1 1 0 1 1 1 5 10 4 3
C 0 0 1 1 1 1 0 0 0 0 5 . 4 .
D 0 0 0 0 0 1 0 0 0 0 . . . .
E 0 1 1 1 0 1 1 1 0 0 4 8 3 3
;
run;

data want;
 set want;
 label
 complete1 = "COMPLETE1 (Month that marks first 3 months of continuous enrollment"
 complete2 = "COMPLETE2 (If there are multiple periods of continuous enrollment, month that marks the next 3 months of continuous enrollment"
 flags1 = "FLAGS1 (Months of coverage for the first enrollment period identified)"
 flags2 = "FLAGS2 (Months of coverage for the second enrollment period identified)"
 ;
run;


data attempt;
 set have;
 length m $500;
 m=cats(of month_01-month_10);
 p='111';
 f=find(m,p);
 if f then complete1=f+length(p)-1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2023 20:20:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872367#M344659</guid>
      <dc:creator>bkq32</dc:creator>
      <dc:date>2023-04-26T20:20:03Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying length of enrollment in a program from indicator variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872387#M344669</link>
      <description>&lt;P&gt;What are you going to do when you have more than 10 months of data? A solution that works for this data set might have to be significantly rewritten for 15.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't clearly define which "month" value you want. It appears to be the LAST of a three month period is that correct?&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2023 20:58:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872387#M344669</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-04-26T20:58:47Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying length of enrollment in a program from indicator variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872389#M344670</link>
      <description>&lt;P&gt;My actual data set has ~200 months of data, and if there happens to be more than 2 periods of at least 3 continuous months of coverage (e.g 111011110111), I'd be fine with just identifying the first two periods. And correct, so for the example below, I want COMPLETE1 to indicate the position of the first [1], and COMPLETE2 to indicate the position of the second [1].&lt;BR /&gt;11[1]011[1]10111&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2023 21:06:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872389#M344670</guid>
      <dc:creator>bkq32</dc:creator>
      <dc:date>2023-04-26T21:06:44Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying length of enrollment in a program from indicator variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872420#M344683</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/288620"&gt;@bkq32&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 1 1 1 1 1 1 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 &amp;gt; 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 &amp;gt; 0) then 
		flag1=findc(str,'0',pos2) - pos2; /* Count number of months */

	/* Find second/additional 3 months of continuous enrollment */
	if (flag1 &amp;gt; 0) then
	do;
		call prxnext(cptrn,start,stop,str,position,length);
		do while(position&amp;gt;0);
			complete2=position+2;
			
			pos2=findc(str,'0',position);
			if ( pos2 &amp;gt; 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 &amp;gt; 0) and (flag1 &amp;lt;=0)) then
		flag1 = 10;
run;
proc print data=want noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2023 00:18:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872420#M344683</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2023-04-27T00:18:59Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying length of enrollment in a program from indicator variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872435#M344695</link>
      <description>&lt;P&gt;Thank you,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13868"&gt;@AhmedAl_Attar&lt;/a&gt;&amp;nbsp;. 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?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 &amp;gt; 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 &amp;gt; 0) then 
		flag1=findc(str,'0',pos2) - pos2; /* Count number of months */

	/* Find second/additional 3 months of continuous enrollment */
	if (flag1 &amp;gt; 0) then
	do;
		call prxnext(cptrn,start,stop,str,position,length);
		do while(position&amp;gt;0);
			complete2=position+2;
			
			pos2=findc(str,'0',position);
			if ( pos2 &amp;gt; 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 &amp;gt; 0) and (flag1 &amp;lt;=0)) then
		flag1 = 10;
run;
proc print data=want (obs=1); run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Apr 2023 03:49:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872435#M344695</guid>
      <dc:creator>bkq32</dc:creator>
      <dc:date>2023-04-27T03:49:27Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying length of enrollment in a program from indicator variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872440#M344697</link>
      <description>&lt;P&gt;This program makes a character string of the month values.&amp;nbsp; It then searches for the leftmost string of '111'.&amp;nbsp; If one is found, it then&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;establishes the corresponding complete value,&lt;/LI&gt;
&lt;LI&gt;replaces the '111' with '000'.&lt;/LI&gt;
&lt;LI&gt;counts subsequent 1's (and resets them to zero) to generate the flag value.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Note that by replacing long string of 1's with 0's for the first enrollment, finding the second enrollment just means searching for the leftmost '111'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can establish more than two enrollments by increasing the &lt;EM&gt;&lt;STRONG&gt;complete&lt;/STRONG&gt;&lt;/EM&gt; and &lt;EM&gt;&lt;STRONG&gt;flag&lt;/STRONG&gt;&lt;/EM&gt; array sizes, and increasing the upper limit of the &lt;EM&gt;&lt;STRONG&gt;DO _i ...&lt;/STRONG&gt;&lt;/EM&gt; loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 1 1 1 1 1 1 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
F 0 0 0 0 0 0 0 1 1 1
;
run;

data want (drop=_:);
  set have;
  array complete {2};
  array flag{2};

  _strng=cats(of month_:) ;

  do _i=1 to 2 while (index(_strng,'111'));
    complete{_i}=index(_strng,'111')+2;
    substr(_strng,complete{_i}-2,3)='000';
    do _c=complete{_i}+1 by 1 while(char(_strng,_c)='1');
      substr(_strng,_c,1)='0';
    end;
    flag{_i}= _c - (complete{_i}-2);
  end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since the length of the character variable &lt;EM&gt;&lt;STRONG&gt;_strng&lt;/STRONG&gt;&lt;/EM&gt; is unspecified, it defaults to 200, meaning it can accommodate up to 199 months.&amp;nbsp; If you specify a specific length for _strng, make sure it is a byte longer than the number of months.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2023 16:29:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872440#M344697</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-04-27T16:29:09Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying length of enrollment in a program from indicator variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872528#M344713</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/288620"&gt;@bkq32&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;to fix this , just change the last if statement as follows&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	if ((complete1 &amp;gt; 0) and (flag1 &amp;lt;=0)) then
		flag1 = (10 - prxmatch(cptrn,str) + 1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But I would suggest you adopt&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;solution. it's a much simpler straightforward solution&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2023 11:18:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872528#M344713</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2023-04-27T11:18:12Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying length of enrollment in a program from indicator variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872535#M344717</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 1 1 1 1 1 1 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;
 set have;
 temp=cats(of month_:);

 pid1=prxparse('/(1{3,})/');
 p1=prxmatch(pid1,temp);
 if p1 then do;
   a = prxposn(pid1, 1, temp);
  complete1=p1+2;
  complete2=.;
  flags1 = length(a);
  flags2 = .;
 end;

 pid2=prxparse('/(1{3,})(0+)(1{3,})/');
 p2=prxmatch(pid2,temp);
 if p2 then do;
   a = prxposn(pid2, 1, temp);
   b = prxposn(pid2, 2, temp);
   c = prxposn(pid2, 3, temp);
  complete2=p2+length(a)+length(b)+2;
  flags2 = length(c);
 end;

 drop pid1 pid2 p1 p2 a b c temp;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Apr 2023 11:46:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872535#M344717</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-04-27T11:46:07Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying length of enrollment in a program from indicator variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872646#M344761</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt; Awesome, thank you for the explanation!</description>
      <pubDate>Thu, 27 Apr 2023 19:12:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-length-of-enrollment-in-a-program-from-indicator/m-p/872646#M344761</guid>
      <dc:creator>bkq32</dc:creator>
      <dc:date>2023-04-27T19:12:41Z</dc:date>
    </item>
  </channel>
</rss>

