<?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: continuous period with a gap in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/continuous-period-with-a-gap/m-p/400569#M97118</link>
    <description>&lt;P&gt;Thank you both for the codes, the second one is much easier to follow.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 03 Oct 2017 12:27:48 GMT</pubDate>
    <dc:creator>lillymaginta</dc:creator>
    <dc:date>2017-10-03T12:27:48Z</dc:date>
    <item>
      <title>continuous period with a gap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/continuous-period-with-a-gap/m-p/400493#M97088</link>
      <description>&lt;P&gt;I have the following data&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;id   start end
1  1/1/2005  2/2/2005
1   2/2/2005  3/3/2005
1   4/4/2005  5/5/2005
2   1/2/2005  2/2/2005
2   2/2/2006  3/3/2006
2   3/3/2006   4/4/2006&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to define continuous enrollment allowing a gap of 31 days.&lt;/P&gt;&lt;P&gt;output&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;id start end
1 1/1/2005  5/5/2005
2 1/2/2005  2/2/2005&lt;BR /&gt;2 2/2/2006  4/4/2006 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 02 Oct 2017 23:56:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/continuous-period-with-a-gap/m-p/400493#M97088</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2017-10-02T23:56:29Z</dc:date>
    </item>
    <item>
      <title>Re: continuous period with a gap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/continuous-period-with-a-gap/m-p/400508#M97093</link>
      <description>&lt;P&gt;I think you need 2 data step2:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;From HAVE, form records, where each record has a single continuous date range, tolerating up to 31 day internal gaps, making data set NEED.&amp;nbsp; Simultaneously track the making number of ranges that any single ID requires, call it _MAX_N&lt;BR /&gt;&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id start :mmddyy10. end :mmddyy10.;
  format start end date9.;
  put (_all_) (=);
datalines;
1  1/1/2005  2/2/2005
1   2/2/2005  3/3/2005
1   4/4/2005  5/5/2005
2   1/2/2005  2/2/2005
2   2/2/2006  3/3/2006
2   3/3/2006   4/4/2006
run;

data need (drop=_:);
  set have nobs=nrecs end=eod1;
  by id;

  if _n_&amp;lt;nrecs then set have (firstobs=2 keep=start rename=(start=_next_start)) ;
  else _next_start=constant('big');

  retain _overall_start _n _max_n ;
  if first.id then do;
    _overall_start=start;
    _n=0;
  end;

  if last.id or _next_start-end&amp;gt;32 then do;
    start=_overall_start;
    _n+1;
    output;
    _overall_start=_next_start;
    _max_n=max(_max_n,_n);
  end;
  if eod1 then call symput('n',cats(_max_n));
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;LI&gt;From NEED, make WANT, with one record per ID, but with _MAX_N pairs of&amp;nbsp;start/end variables.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  do _i=1 by 1 until (last.id);
    set need;
    by id;
    array sd {&amp;amp;n} start1-start&amp;amp;n;
    array ed {&amp;amp;n} end1-end&amp;amp;n;
    format start: end: date9. ;
    sd{_i}=start;
    ed{_i}=end;
  end;
  drop start end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;In the first program there is a second SET statement with &lt;EM&gt;&lt;STRONG&gt;firstobs=2&lt;/STRONG&gt;&lt;/EM&gt; option, so that there is always a single record lookahead to provide the value for next_start.&amp;nbsp; At the end of processing _MAX_N is assign to macrovar _N, which gets used in the DATA WANT step.&amp;nbsp; The second program has SET inside a "do ... unitl (last.id)" loop so that each sequential record can have its values&amp;nbsp;ssigned to the corresponding element of the SD and ED arrays.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Oct 2017 02:32:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/continuous-period-with-a-gap/m-p/400508#M97093</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-10-03T02:32:34Z</dc:date>
    </item>
    <item>
      <title>Re: continuous period with a gap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/continuous-period-with-a-gap/m-p/400535#M97103</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/72105"&gt;@lillymaginta&lt;/a&gt;&lt;/P&gt;&lt;P&gt;This should do it:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
informat start end mmddyy10.;
format start end date9.;
input id start end;
cards;
1  1/1/2005  2/2/2005
1   2/2/2005  3/3/2005
1   4/4/2005  5/5/2005
2   1/2/2005  2/2/2005
2   2/2/2006  3/3/2006
2   3/3/2006   4/4/2006
;run;

data want;
  set have;
  by id;
  if first.id then
    start0=start; /* start a new period */
  else if start-lag_end&amp;gt;31 then do;
    output; /* output previous period */
    start0=start;  /* start a new period */
    end;&lt;BR /&gt;  lag_end=end;
  if last.id then
    output;  /* output last period */
  retain start0 lag_end;
  drop start end;
  rename start0=start lag_end=end;
  format start0 lag_end date9.;
run;

&lt;/PRE&gt;&lt;P&gt;Basically, Start0 is used to store the beginning of the output interval, lag_end is used to store the previous value of End. These two values become the new Start and End.&lt;/P&gt;&lt;P&gt;It seems that your stipulated output is not correct, though, as there are 32 days from&amp;nbsp;March 3rd to April 4th (so there are two ranges for ID=1).&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Oct 2017 09:25:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/continuous-period-with-a-gap/m-p/400535#M97103</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2017-10-03T09:25:54Z</dc:date>
    </item>
    <item>
      <title>Re: continuous period with a gap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/continuous-period-with-a-gap/m-p/400569#M97118</link>
      <description>&lt;P&gt;Thank you both for the codes, the second one is much easier to follow.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Oct 2017 12:27:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/continuous-period-with-a-gap/m-p/400569#M97118</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2017-10-03T12:27:48Z</dc:date>
    </item>
    <item>
      <title>Re: continuous period with a gap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/continuous-period-with-a-gap/m-p/400581#M97120</link>
      <description>&lt;P&gt;Assuming there are not missing value in START or END variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
informat start end mmddyy10.;
format start end date9.;
input id start end;
cards;
1  1/1/2005  2/2/2005
1   2/2/2005  3/3/2005
1   4/4/2005  5/5/2005
2   1/2/2005  2/2/2005
2   2/2/2006  3/3/2006
2   3/3/2006   4/4/2006
;
run;
data temp;
 set have;
 by id;
 if first.id or start-lag(end)&amp;gt;32 then group+1;
run;
data want;
 set temp(rename=(start=_start));
 by group;
 retain start;
 if first.group then start=_start;
 if last.group then output;
 format start date9.;
 drop _start;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Oct 2017 13:03:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/continuous-period-with-a-gap/m-p/400581#M97120</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-10-03T13:03:38Z</dc:date>
    </item>
    <item>
      <title>Re: continuous period with a gap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/continuous-period-with-a-gap/m-p/401149#M97284</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Oct 2017 20:13:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/continuous-period-with-a-gap/m-p/401149#M97284</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2017-10-04T20:13:44Z</dc:date>
    </item>
  </channel>
</rss>

