<?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: Count continuous months allowing one gap in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726773#M225889</link>
    <description>Certainly. I'll update the code to include the &amp;gt;= operator.</description>
    <pubDate>Tue, 16 Mar 2021 14:19:27 GMT</pubDate>
    <dc:creator>maguiremq</dc:creator>
    <dc:date>2021-03-16T14:19:27Z</dc:date>
    <item>
      <title>Count continuous months allowing one gap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726521#M225751</link>
      <description>&lt;P&gt;Dear SAS community fellows,&lt;/P&gt;&lt;P&gt;Allow me to delve straight into topic: Let's say a group of patients were supposed to attend a once-per-month consultation&lt;SPAN style="font-family: inherit;"&gt;&amp;nbsp;for at least 6 months straight last year. They were allowed to skip one session at most during the 6-month period if they wanted to do so. So here is the data, where "month" is the month when the patient came in for the consultation, and "monthcnt" is the _N_ for each patient.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input patient $ 1&amp;nbsp;@3 month monthcnt;
datalines;
A 1 1
A 2 2
A 3 3
A 4 4
A 5 5
A 6 6
B 1 1
B 3 2
B 4 3
B 5 4
B 6 5
C 7 1
C 8 2
C 9 3
D 1 1
D 4 2
D 5 3
D 6 4
D 7 5
D 8 6
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;Thanks to the program coded by Zaizai Lu and David Shen in their poster about SAS array (&lt;A href="https://support.sas.com/resources/papers/proceedings/proceedings/sugi31/156-31.pdf" target="_blank" rel="noopener"&gt;https://support.sas.com/resources/papers/proceedings/proceedings/sugi31/156-31.pdf&lt;/A&gt;), I was able to obtain patient A, who had 6 consecutive monthly consultations, by running the code below. But I don't know how to obtain patient B and D, who skipped one consultation during a 6-month period. Any insight on how to achieve this will be welcome!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*monthly consultation by patient;
proc transpose data=have prefix=mo out=t;
by patient;
var month;
run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data&amp;nbsp;ind&amp;nbsp;(keep=patient&amp;nbsp;flag&amp;nbsp;count&amp;nbsp;i&amp;nbsp;rename=(i=monthcnt));
set&amp;nbsp;t;
array&amp;nbsp;mth&amp;nbsp;{*}&amp;nbsp;mo:&amp;nbsp;dummy;
retain&amp;nbsp;flag&amp;nbsp;count&amp;nbsp;1;
do&amp;nbsp;i=1&amp;nbsp;to&amp;nbsp;dim(mth)-1;
 &amp;nbsp;if&amp;nbsp;mth[i]^=.&amp;nbsp;then&amp;nbsp;do;
 &amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;mth[i]&amp;nbsp;=&amp;nbsp;mth[i+1]-1&amp;nbsp;then&amp;nbsp;do;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;output;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;count = count+1;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;do;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;output;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;flag&amp;nbsp;= flag+1;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;count = 1;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end;
&amp;nbsp;&amp;nbsp;end;
end;
run;&lt;/CODE&gt;&lt;/PRE&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&amp;nbsp;temp1;
merge&amp;nbsp;ind&amp;nbsp;have;
by&amp;nbsp;patient&amp;nbsp;monthcnt;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*6 consecutive monthly consultation;
data&amp;nbsp;continue (where=(count &amp;gt; = 6));
set&amp;nbsp;temp1;
by&amp;nbsp;patient&amp;nbsp;flag;
retain&amp;nbsp;f_month;
if&amp;nbsp;first.flag&amp;nbsp;then&amp;nbsp;f_month=month;
if&amp;nbsp;last.flag&amp;nbsp;then&amp;nbsp;do;
&amp;nbsp;&amp;nbsp;l_month=month;
&amp;nbsp;&amp;nbsp;output;
end;
keep&amp;nbsp;patient&amp;nbsp;f_month&amp;nbsp;l_month&amp;nbsp;count;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Mar 2021 19:54:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726521#M225751</guid>
      <dc:creator>aaronh</dc:creator>
      <dc:date>2021-03-15T19:54:14Z</dc:date>
    </item>
    <item>
      <title>Re: Count continuous months allowing one gap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726535#M225760</link>
      <description>&lt;P&gt;What do you want your data to look like? I'm getting a little lost between all the output statements and arrays. That code seems a bit convoluted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This flags whether a month was skipped, but I couldn't provide an answer without knowing what you want it to ultimately look like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input patient $ 1 @3 month monthcnt;
datalines;
A 1 1
A 2 2
A 3 3
A 4 4
A 5 5
A 6 6
B 1 1
B 3 2
B 4 3
B 5 4
B 6 5
C 7 1
C 8 2
C 9 3
D 1 1
D 4 2
D 5 3
D 6 4
D 7 5
D 8 6
;

data have_2;
	set have;
	by patient month;
		lag_month = lag(month);
		if first.patient then call missing(lag_month);
		if not first.patient then do;
			if month - lag_month ^= 1 then skip_month = 1;
			else skip_month = 0;
		end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could use PROC MEANS to get the max for each ID and then merge it back. That or PROC SQL. Nonetheless, please also provide a DATALINES statement with your 'want' data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Mar 2021 20:39:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726535#M225760</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-03-15T20:39:43Z</dc:date>
    </item>
    <item>
      <title>Re: Count continuous months allowing one gap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726545#M225764</link>
      <description>&lt;P&gt;Having been involved in followup medical appointments I really think that &lt;STRONG&gt;dates&lt;/STRONG&gt; of visit should be involved so the folks that start in October will have something that makes since when January comes around. Unless you claim every patient involved in this program starts before June of the year. Every year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may also want to notice that the difference between visit number and the months-from-start interval is the number of skipped months&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;D 1 1   1-1 = 0 skipped months
D 4 2   4-2 = 2 skipped months (month numbers 2 and 3)&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 15 Mar 2021 21:19:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726545#M225764</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-03-15T21:19:50Z</dc:date>
    </item>
    <item>
      <title>Re: Count continuous months allowing one gap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726552#M225768</link>
      <description>Indeed... what I have given here really is a representation of what I actually have - enrollment data, but I was really trying to give an example. You are absolutely correct that a patient could start in July and finish in Dec.</description>
      <pubDate>Mon, 15 Mar 2021 21:51:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726552#M225768</guid>
      <dc:creator>aaronh</dc:creator>
      <dc:date>2021-03-15T21:51:31Z</dc:date>
    </item>
    <item>
      <title>Re: Count continuous months allowing one gap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726554#M225770</link>
      <description>I only need the patient ID in this case. So for this particular data, I want patient A, B, and D. Patient A has 6 consecutive visits, B has 5 in 6 months (Jan - Jun), and patient D also has 5 in 6 months (Apr - Sep, or Mar - Aug, depending on how to look at it).</description>
      <pubDate>Mon, 15 Mar 2021 21:54:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726554#M225770</guid>
      <dc:creator>aaronh</dc:creator>
      <dc:date>2021-03-15T21:54:01Z</dc:date>
    </item>
    <item>
      <title>Re: Count continuous months allowing one gap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726748#M225874</link>
      <description>&lt;P&gt;This may need to be modified in light of the comments above, but this gives what you requested, but it looks a little ugly in my opinion.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input patient $ 1 @3 month monthcnt;
datalines;
A 1 1
A 2 2
A 3 3
A 4 4
A 5 5
A 6 6
B 1 1
B 3 2
B 4 3
B 5 4
B 6 5
C 7 1
C 8 2
C 9 3
D 1 1
D 4 2
D 5 3
D 6 4
D 7 5
D 8 6
;

data have_2;
	set have;
	by patient month;
		lag_month = lag(month);
		if first.patient then call missing(lag_month);
		if not first.patient then do;
			if month - lag_month ^= 1 then skip_month = 1;
			else skip_month = 0;
		end;
run;

proc summary data = have_2 noprint;
	class patient; /* create table with patient as the identifier */
	var skip_month; /* create column based on whether the individual has a skip month */
	ways 1; /* eliminate default summary row for all records */
	output out = want (drop = _type_
					   where = (total_months &amp;gt;= 6 or (total_months = 5 and skip_month = 1)) 
					   rename = (_freq_ = total_months)) /* (1) drop _type_, (2) subset based on months and skip_month, (3) rename default column _freq_ to total_months just so it looks nicer */
		max = ; /* output the max skip_month for an individual */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;Obs patient total_months skip_month 
1 A 6 0 
2 B 5 1 
3 D 6 1 
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 14:20:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726748#M225874</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-03-16T14:20:07Z</dc:date>
    </item>
    <item>
      <title>Re: Count continuous months allowing one gap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726763#M225881</link>
      <description>&lt;P&gt;Arrays are useful, but after building the array, convert it to a string of 1's and .'s.&amp;nbsp; Then search (using the FIND function) for 5 consecutive ones, or else 5 ones and a single . in 6 consecutive positions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input patient $ 1 @3 month monthcnt;
datalines;
A 1 1
A 2 2
A 3 3
A 4 4
A 5 5
A 6 6
B 1 1
B 3 2
B 4 3
B 5 4
B 6 5
C 7 1
C 8 2
C 9 3
D 1 1
D 4 2
D 5 3
D 6 4
D 7 5
D 8 6
;
data want (drop=_:);
  array _mnths {20} ;
  do until (last.patient);
    set have ;
    by patient;
    _mnths{month}=1;
  end;
  _strng=cats(of _mnths{*});
  do _srch='11111','1.1111','11.111','111.11','1111.1' until (_f^=0);
   _f=find(_strng,_srch);
  end;

  do until (last.patient);  /* Reread and (optionally) output this patient*/
    set have;
    by patient;
    if _f^=0 then output;
  end;
run;&lt;BR /&gt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if you are worried about indexing multiple months over consecutive years, then just declare a 2-way array, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  array mnths {2014:2016,1:12} ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can populate the array via statements like:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  mnths{year,month}=1;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The CATS function will concatenate the 1's and .'s in chronological order (assuming the major index is year and the minor index is month), so the rest of the code works unmodified.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 13:57:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726763#M225881</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-16T13:57:24Z</dc:date>
    </item>
    <item>
      <title>Re: Count continuous months allowing one gap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726772#M225888</link>
      <description>Thank you! This is very cool... though if I've understood the code correctly, it may require some more tweaking when a patient visited more than 6 months in a row.</description>
      <pubDate>Tue, 16 Mar 2021 14:16:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726772#M225888</guid>
      <dc:creator>aaronh</dc:creator>
      <dc:date>2021-03-16T14:16:45Z</dc:date>
    </item>
    <item>
      <title>Re: Count continuous months allowing one gap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726773#M225889</link>
      <description>Certainly. I'll update the code to include the &amp;gt;= operator.</description>
      <pubDate>Tue, 16 Mar 2021 14:19:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726773#M225889</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-03-16T14:19:27Z</dc:date>
    </item>
    <item>
      <title>Re: Count continuous months allowing one gap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726776#M225891</link>
      <description>Interestingly, one of my colleagues suggested me something that's essentially what you've put down here. It may be a bit challenging to search using an array directly, but if we concatenate all of the indicator variables into one variable, and then use the find() function, it becomes way less complicated. We just need to take note to exhaust all of the possible combinations of the '1111.1' etc etc. Thank you.</description>
      <pubDate>Tue, 16 Mar 2021 14:21:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-continuous-months-allowing-one-gap/m-p/726776#M225891</guid>
      <dc:creator>aaronh</dc:creator>
      <dc:date>2021-03-16T14:21:02Z</dc:date>
    </item>
  </channel>
</rss>

