<?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: 12 months of continuous enrollment before and after a reference date (multiple rows per id) in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/12-months-of-continuous-enrollment-before-and-after-a-reference/m-p/248663#M13083</link>
    <description>&lt;P&gt;Another option. Note, both this and PG's need a LOT of testing...these are always very tricky!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;informat start_e end_e date_h mmddyy10.;&lt;BR /&gt;format start_e end_e date_h date.;&lt;BR /&gt;input id start_e end_e date_h;&lt;BR /&gt;cards;&lt;BR /&gt;1 1/1/2005 1/1/2006&amp;nbsp; 2/8/2008&lt;BR /&gt;1 2/3/2006 4/5/2013 .&lt;BR /&gt;2 5/7/2005 8/8/2006&amp;nbsp; 4/5/2007&lt;BR /&gt;2 1/1/2007 2/2/2012 .&lt;BR /&gt;3 5/9/2005 5/9/2007&amp;nbsp; 1/1/2007&lt;BR /&gt;3 6/4/2008 7/7/2012 .&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;/* StartYear is a year prior to your first date, YearCount encompasses all of the years in your list */&lt;BR /&gt;%let StartYear = 2000;&lt;BR /&gt;%let YearCount = 20; /* Max 85 years */&lt;/P&gt;&lt;P&gt;data _null_; /* Set some macro variables */&lt;BR /&gt;StartDate = mdy(1, 1, &amp;amp;StartYear.); /* Jan 1 of start year */&lt;BR /&gt;EndDate = intnx('year', StartDate, (&amp;amp;YearCount - 1), 'end'); /* Dec 31&amp;nbsp; of end year */&lt;BR /&gt;NumberOfDays = intck('day', StartDate, EndDate) + 1; /* Number of days between them, inclusive */&lt;BR /&gt;call symput('StartDate', put(StartDate, 8.));&lt;BR /&gt;call symput('EndDate', put(EndDate, 8.));&lt;BR /&gt;call symput('NumberOfDays', put(NumberOfDays, 8.));&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data HDates; /* Pull out the date_h information into a separate dataset */&lt;BR /&gt;set have;&lt;BR /&gt;keep id date_h;&lt;BR /&gt;if ^missing(date_h);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sort data=have(drop=date_h); /* Sort ranges by ID, for BY processing */&lt;BR /&gt;by id;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data GoodDates; /* Set an array of 1's for the dates in the ranges */&lt;BR /&gt;length DateList $&amp;amp;NumberOfDays.;&lt;BR /&gt;drop _: start_e end_e;&lt;BR /&gt;set have;&lt;BR /&gt;by id;&lt;BR /&gt;if first.id /* zero out the array at the beginning of an id */&lt;BR /&gt;then DateList = repeat('0', &amp;amp;NumberOfDays. - 1);&lt;/P&gt;&lt;P&gt;_Start = start_e - &amp;amp;StartDate. + 1; /* Adjust the dates so that start date is 1, to use with the character variable */&lt;BR /&gt;_NumDays = end_e - start_e + 1;&lt;BR /&gt;substr(DateList, _Start, _Numdays) = repeat('1', _Numdays - 1); /* for all of the days in the range, set the corresponding bytes to 1 */&lt;/P&gt;&lt;P&gt;if last.id /* Finished with this id */&lt;BR /&gt;then output;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sql; /* Merge the date ranges with the date_h values */&lt;BR /&gt;create table CombinedDates&lt;BR /&gt;as select GoodDates.*, HDates.date_h&lt;BR /&gt;from GoodDates inner join HDates on GoodDates.id = HDates.id;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;drop _: DateList;&lt;BR /&gt;set CombinedDates;&lt;BR /&gt;_Start = intnx('year', date_h, -1, 'same') - &amp;amp;StartDate. + 1; /* get the start location and the length of 1 year either side of date_h */&lt;BR /&gt;_End = intnx('year', date_h, 1, 'same') - &amp;amp;StartDate. + 1;&lt;BR /&gt;_NumDays = _End - _Start + 1;&lt;BR /&gt;if substr(DateList, _Start, _Numdays) = repeat('1', _Numdays - 1) /* Check if the character variable is all 1s for the desired locations */&lt;BR /&gt;then GoodRecord = 1;&lt;BR /&gt;else GoodRecord = 0;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Mon, 08 Feb 2016 14:36:44 GMT</pubDate>
    <dc:creator>TomKari</dc:creator>
    <dc:date>2016-02-08T14:36:44Z</dc:date>
    <item>
      <title>12 months of continuous enrollment before and after a reference date (multiple rows per id)</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/12-months-of-continuous-enrollment-before-and-after-a-reference/m-p/248583#M13077</link>
      <description>&lt;P&gt;i am &amp;nbsp;trying to find id with 12 months of continous enrollment (no gap) before and after date_h. Each id can have multiple enrollment period (several start_e and end_e dates). In this example only id 1 should be included (have 12 months before and 12 months after).&lt;/P&gt;&lt;P&gt;id 2 will be excluded because there was a gap in coverage between 8/8/2006 and 1/1/2007&lt;/P&gt;&lt;P&gt;id 3 will be excluded because there was 12 months of continous enrollment before but not after date_h&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 
id start_e end_e     date_h
1 1/1/2005 1/1/2006  2/8/2008
1 2/3/2006 4/5/2013
2 5/7/2005 8/8/2006  4/5/2007
2 1/1/2007 2/2/2012
3 5/9/2005 5/9/2007  1/1/2007
3 6/4/2008 7/7/2012&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Feb 2016 21:52:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/12-months-of-continuous-enrollment-before-and-after-a-reference/m-p/248583#M13077</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2016-02-07T21:52:39Z</dc:date>
    </item>
    <item>
      <title>Re: 12 months of continuous enrollment before and after a reference date (multiple rows per id)</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/12-months-of-continuous-enrollment-before-and-after-a-reference/m-p/248614#M13081</link>
      <description>&lt;P&gt;Use a self join:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input id (start_e end_e date_h) (:mmddyy10.);
format start_e end_e date_h yymmdd10.;
datalines;
1 1/1/2005 1/1/2006  2/8/2008
1 2/3/2006 4/5/2013
2 5/7/2005 8/8/2006  4/5/2007
2 1/1/2007 2/2/2012
3 5/9/2005 5/9/2007  1/1/2007
3 6/4/2008 7/7/2012
;

proc sql;
create table want as
select a.id, a.date_h, b.start_e, b.end_e
from have as a inner join have as b
    on  a.id=b.id and
        b.start_e &amp;lt;= intnx("MONTH", a.date_h, -12, "SAME") and
        b.end_e   &amp;gt;= intnx("MONTH", a.date_h,  12, "SAME")
where a.date_h is not missing;
select * from want;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Feb 2016 03:19:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/12-months-of-continuous-enrollment-before-and-after-a-reference/m-p/248614#M13081</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-02-08T03:19:40Z</dc:date>
    </item>
    <item>
      <title>Re: 12 months of continuous enrollment before and after a reference date (multiple rows per id)</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/12-months-of-continuous-enrollment-before-and-after-a-reference/m-p/248663#M13083</link>
      <description>&lt;P&gt;Another option. Note, both this and PG's need a LOT of testing...these are always very tricky!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;informat start_e end_e date_h mmddyy10.;&lt;BR /&gt;format start_e end_e date_h date.;&lt;BR /&gt;input id start_e end_e date_h;&lt;BR /&gt;cards;&lt;BR /&gt;1 1/1/2005 1/1/2006&amp;nbsp; 2/8/2008&lt;BR /&gt;1 2/3/2006 4/5/2013 .&lt;BR /&gt;2 5/7/2005 8/8/2006&amp;nbsp; 4/5/2007&lt;BR /&gt;2 1/1/2007 2/2/2012 .&lt;BR /&gt;3 5/9/2005 5/9/2007&amp;nbsp; 1/1/2007&lt;BR /&gt;3 6/4/2008 7/7/2012 .&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;/* StartYear is a year prior to your first date, YearCount encompasses all of the years in your list */&lt;BR /&gt;%let StartYear = 2000;&lt;BR /&gt;%let YearCount = 20; /* Max 85 years */&lt;/P&gt;&lt;P&gt;data _null_; /* Set some macro variables */&lt;BR /&gt;StartDate = mdy(1, 1, &amp;amp;StartYear.); /* Jan 1 of start year */&lt;BR /&gt;EndDate = intnx('year', StartDate, (&amp;amp;YearCount - 1), 'end'); /* Dec 31&amp;nbsp; of end year */&lt;BR /&gt;NumberOfDays = intck('day', StartDate, EndDate) + 1; /* Number of days between them, inclusive */&lt;BR /&gt;call symput('StartDate', put(StartDate, 8.));&lt;BR /&gt;call symput('EndDate', put(EndDate, 8.));&lt;BR /&gt;call symput('NumberOfDays', put(NumberOfDays, 8.));&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data HDates; /* Pull out the date_h information into a separate dataset */&lt;BR /&gt;set have;&lt;BR /&gt;keep id date_h;&lt;BR /&gt;if ^missing(date_h);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sort data=have(drop=date_h); /* Sort ranges by ID, for BY processing */&lt;BR /&gt;by id;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data GoodDates; /* Set an array of 1's for the dates in the ranges */&lt;BR /&gt;length DateList $&amp;amp;NumberOfDays.;&lt;BR /&gt;drop _: start_e end_e;&lt;BR /&gt;set have;&lt;BR /&gt;by id;&lt;BR /&gt;if first.id /* zero out the array at the beginning of an id */&lt;BR /&gt;then DateList = repeat('0', &amp;amp;NumberOfDays. - 1);&lt;/P&gt;&lt;P&gt;_Start = start_e - &amp;amp;StartDate. + 1; /* Adjust the dates so that start date is 1, to use with the character variable */&lt;BR /&gt;_NumDays = end_e - start_e + 1;&lt;BR /&gt;substr(DateList, _Start, _Numdays) = repeat('1', _Numdays - 1); /* for all of the days in the range, set the corresponding bytes to 1 */&lt;/P&gt;&lt;P&gt;if last.id /* Finished with this id */&lt;BR /&gt;then output;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sql; /* Merge the date ranges with the date_h values */&lt;BR /&gt;create table CombinedDates&lt;BR /&gt;as select GoodDates.*, HDates.date_h&lt;BR /&gt;from GoodDates inner join HDates on GoodDates.id = HDates.id;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;drop _: DateList;&lt;BR /&gt;set CombinedDates;&lt;BR /&gt;_Start = intnx('year', date_h, -1, 'same') - &amp;amp;StartDate. + 1; /* get the start location and the length of 1 year either side of date_h */&lt;BR /&gt;_End = intnx('year', date_h, 1, 'same') - &amp;amp;StartDate. + 1;&lt;BR /&gt;_NumDays = _End - _Start + 1;&lt;BR /&gt;if substr(DateList, _Start, _Numdays) = repeat('1', _Numdays - 1) /* Check if the character variable is all 1s for the desired locations */&lt;BR /&gt;then GoodRecord = 1;&lt;BR /&gt;else GoodRecord = 0;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2016 14:36:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/12-months-of-continuous-enrollment-before-and-after-a-reference/m-p/248663#M13083</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2016-02-08T14:36:44Z</dc:date>
    </item>
  </channel>
</rss>

