<?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 Check if date points fall in the time intervals for eligibility assessment in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Check-if-date-points-fall-in-the-time-intervals-for-eligibility/m-p/548998#M152282</link>
    <description>&lt;P&gt;Hi guys,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd like to mark the patients who had medical insurance when he/she was diagnosed (date_diagnosis) with the disease under the study (KEEP2=YES). All patients have insurance on and off basis, however, some patients had no insurance coverage when the disease was diagnosed (KEEP2=NO). My data has date pairs for the intervals of insurance coverage., ie., start1-end1 and patient lost the insurance and got it back during start2-end2 ...so forth so on.&amp;nbsp; Desired output is the KEEP_CORRECT. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This problem was resolved by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt; and worked on the data I had back then. However, the algorithm makes wrong choice as I&lt;/P&gt;
&lt;P&gt;have shown in the image attached. The red stars point out where date_diagnosis actually fell in the intervals of insurance coverage&lt;/P&gt;
&lt;P&gt;that I would mark KEEP2=YES but algorithm creates KEEP2=NO.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CORRECT ELIG ALGORITHM.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/28515i1C6135FBC32C0CCF/image-size/large?v=v2&amp;amp;px=999" role="button" title="CORRECT ELIG ALGORITHM.png" alt="CORRECT ELIG ALGORITHM.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please help fix the algorithm? or suggest alternative solutions, if any?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Select-the-cases-which-included-the-full-calendar-year-based-on/td-p/515691" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/Select-the-cases-which-included-the-full-calendar-year-based-on/td-p/515691&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another wrinkle is the repeated data. The algorithm works well with unique rows. However, my actual data has duplicates. I think I should create another forum once algorithm works for the data with distinct rows. &lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA ELIGIBILITY; 
INPUT ID start1 end1 start2 end2 start3 end3 start4 end4 start5 end5 DATE_DIAGNOSIS DATE_DEATH YEAR KEEP_CORRECT $;
Informat start1 end1 start2 end2 start3 end3 start4 end4 start5 end5 DATE_DIAGNOSIS DATE_DEATH anydtdte.;
format start1 end1 start2 end2 start3 end3 start4 end4 start5 end5 DATE_DIAGNOSIS DATE_DEATH date9.;
CARDS;
1	1-Feb-04	28-Feb-06	1-Apr-07	31-May-10	1-Nov-10	31-Aug-13	1-Oct-14	30-Sep-15	1-Mar-16	31-Dec-16	4-Jan-06	.	2006	YES
2	1-Jan-02	31-Dec-04	1-Feb-09	30-Apr-10	1-Jul-10	31-Oct-10	1-Nov-12	31-Dec-16	1-Jul-17	30-Nov-17	23-Feb-09	.	2009	YES
3	1-May-05	31-Jul-05	.	.	.	.	.	.	.	.	1-Mar-05	16633	2005	NO
4	1-Sep-05	30-Sep-05	.	.	.	.	.	.	.	.	29-Jun-05	16701	2005	NO
5	1-Apr-05	30-Apr-07	.	.	.	.	.	.	.	.	3-Jan-05	18629	2005	NO
6	1-Feb-05	31-Oct-05	.	.	.	.	.	.	.	.	3-Jan-05	.	2005	NO
7	1-Apr-04	31-Jul-04	1-Jan-05	30-Jun-05	1-Aug-05	31-Dec-06	.	.	.	.	4-Jan-05	16901	2005	YES
8	1-Sep-05	30-Nov-05	.	.	.	.	.	.	.	.	5-Jan-05	16716	2005	NO
9	1-Jul-05	31-Dec-05	.	.	.	.	.	.	.	.	7-Jan-05	17332	2005	NO
10	1-Jun-04	30-Jun-05	.	.	.	.	.	.	.	.	13-Jan-05	16827	2005	YES
;

data want (drop=_:);
  set eligibility;
  array st {*} start: ;
  array en {*} end:   ;

  _np=n(of st{*});                              /* N of start/end pairs */
  _upper_date=min(mdy(12,31,year),date_death);  /* Ins must cover through this date */

  keep2='YES';                                  /* Assume eligiblity */
  if st{1} &amp;gt; mdy(1,1,year) then keep2='NO ';       /*minimum start is too late ...*/
  else if en{_np} &amp;lt; _upper_date then keep2='NO ';  /*max end precedes upper date*/

  /* Check every gap (i.e. end1:start2,  end2:start3) preceding upper date */
  else if _np&amp;gt;1 then do _i=1 to _np-1 while (en{_i}&amp;lt;_upper_date and keep2='YES'); 
    if year(en{_i}+1)=year then keep2='NO';
    else if year(st{_i+1}-1)=year then keep2='NO';
  end;

run;

PROC PRINT DATA=WANT; RUN; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 05 Apr 2019 23:23:08 GMT</pubDate>
    <dc:creator>Cruise</dc:creator>
    <dc:date>2019-04-05T23:23:08Z</dc:date>
    <item>
      <title>Check if date points fall in the time intervals for eligibility assessment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-date-points-fall-in-the-time-intervals-for-eligibility/m-p/548998#M152282</link>
      <description>&lt;P&gt;Hi guys,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd like to mark the patients who had medical insurance when he/she was diagnosed (date_diagnosis) with the disease under the study (KEEP2=YES). All patients have insurance on and off basis, however, some patients had no insurance coverage when the disease was diagnosed (KEEP2=NO). My data has date pairs for the intervals of insurance coverage., ie., start1-end1 and patient lost the insurance and got it back during start2-end2 ...so forth so on.&amp;nbsp; Desired output is the KEEP_CORRECT. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This problem was resolved by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt; and worked on the data I had back then. However, the algorithm makes wrong choice as I&lt;/P&gt;
&lt;P&gt;have shown in the image attached. The red stars point out where date_diagnosis actually fell in the intervals of insurance coverage&lt;/P&gt;
&lt;P&gt;that I would mark KEEP2=YES but algorithm creates KEEP2=NO.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CORRECT ELIG ALGORITHM.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/28515i1C6135FBC32C0CCF/image-size/large?v=v2&amp;amp;px=999" role="button" title="CORRECT ELIG ALGORITHM.png" alt="CORRECT ELIG ALGORITHM.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please help fix the algorithm? or suggest alternative solutions, if any?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Select-the-cases-which-included-the-full-calendar-year-based-on/td-p/515691" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/Select-the-cases-which-included-the-full-calendar-year-based-on/td-p/515691&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another wrinkle is the repeated data. The algorithm works well with unique rows. However, my actual data has duplicates. I think I should create another forum once algorithm works for the data with distinct rows. &lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA ELIGIBILITY; 
INPUT ID start1 end1 start2 end2 start3 end3 start4 end4 start5 end5 DATE_DIAGNOSIS DATE_DEATH YEAR KEEP_CORRECT $;
Informat start1 end1 start2 end2 start3 end3 start4 end4 start5 end5 DATE_DIAGNOSIS DATE_DEATH anydtdte.;
format start1 end1 start2 end2 start3 end3 start4 end4 start5 end5 DATE_DIAGNOSIS DATE_DEATH date9.;
CARDS;
1	1-Feb-04	28-Feb-06	1-Apr-07	31-May-10	1-Nov-10	31-Aug-13	1-Oct-14	30-Sep-15	1-Mar-16	31-Dec-16	4-Jan-06	.	2006	YES
2	1-Jan-02	31-Dec-04	1-Feb-09	30-Apr-10	1-Jul-10	31-Oct-10	1-Nov-12	31-Dec-16	1-Jul-17	30-Nov-17	23-Feb-09	.	2009	YES
3	1-May-05	31-Jul-05	.	.	.	.	.	.	.	.	1-Mar-05	16633	2005	NO
4	1-Sep-05	30-Sep-05	.	.	.	.	.	.	.	.	29-Jun-05	16701	2005	NO
5	1-Apr-05	30-Apr-07	.	.	.	.	.	.	.	.	3-Jan-05	18629	2005	NO
6	1-Feb-05	31-Oct-05	.	.	.	.	.	.	.	.	3-Jan-05	.	2005	NO
7	1-Apr-04	31-Jul-04	1-Jan-05	30-Jun-05	1-Aug-05	31-Dec-06	.	.	.	.	4-Jan-05	16901	2005	YES
8	1-Sep-05	30-Nov-05	.	.	.	.	.	.	.	.	5-Jan-05	16716	2005	NO
9	1-Jul-05	31-Dec-05	.	.	.	.	.	.	.	.	7-Jan-05	17332	2005	NO
10	1-Jun-04	30-Jun-05	.	.	.	.	.	.	.	.	13-Jan-05	16827	2005	YES
;

data want (drop=_:);
  set eligibility;
  array st {*} start: ;
  array en {*} end:   ;

  _np=n(of st{*});                              /* N of start/end pairs */
  _upper_date=min(mdy(12,31,year),date_death);  /* Ins must cover through this date */

  keep2='YES';                                  /* Assume eligiblity */
  if st{1} &amp;gt; mdy(1,1,year) then keep2='NO ';       /*minimum start is too late ...*/
  else if en{_np} &amp;lt; _upper_date then keep2='NO ';  /*max end precedes upper date*/

  /* Check every gap (i.e. end1:start2,  end2:start3) preceding upper date */
  else if _np&amp;gt;1 then do _i=1 to _np-1 while (en{_i}&amp;lt;_upper_date and keep2='YES'); 
    if year(en{_i}+1)=year then keep2='NO';
    else if year(st{_i+1}-1)=year then keep2='NO';
  end;

run;

PROC PRINT DATA=WANT; RUN; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Apr 2019 23:23:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-date-points-fall-in-the-time-intervals-for-eligibility/m-p/548998#M152282</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2019-04-05T23:23:08Z</dc:date>
    </item>
    <item>
      <title>Re: Check if date points fall in the time intervals for eligibility assessment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-date-points-fall-in-the-time-intervals-for-eligibility/m-p/549010#M152288</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/132289"&gt;@Cruise&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When working with date ranges using SQL with data organized in a long format makes things often much simpler.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here how this could look like.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA wide;
  INPUT ID start1 end1 start2 end2 start3 end3 start4 end4 start5 end5 DATE_DIAGNOSIS DATE_DEATH YEAR KEEP_CORRECT $;
  Informat start1 end1 start2 end2 start3 end3 start4 end4 start5 end5 DATE_DIAGNOSIS DATE_DEATH anydtdte.;
  format start1 end1 start2 end2 start3 end3 start4 end4 start5 end5 DATE_DIAGNOSIS DATE_DEATH date9.;
  CARDS;
1 1-Feb-04  28-Feb-06 1-Apr-07  31-May-10 1-Nov-10  31-Aug-13 1-Oct-14  30-Sep-15 1-Mar-16  31-Dec-16 4-Jan-06  . 2006  YES
2 1-Jan-02  31-Dec-04 1-Feb-09  30-Apr-10 1-Jul-10  31-Oct-10 1-Nov-12  31-Dec-16 1-Jul-17  30-Nov-17 23-Feb-09 . 2009  YES
3 1-May-05  31-Jul-05 . . . . . . . . 1-Mar-05  16633 2005  NO
4 1-Sep-05  30-Sep-05 . . . . . . . . 29-Jun-05 16701 2005  NO
5 1-Apr-05  30-Apr-07 . . . . . . . . 3-Jan-05  18629 2005  NO
6 1-Feb-05  31-Oct-05 . . . . . . . . 3-Jan-05  . 2005  NO
7 1-Apr-04  31-Jul-04 1-Jan-05  30-Jun-05 1-Aug-05  31-Dec-06 . . . . 4-Jan-05  16901 2005  YES
8 1-Sep-05  30-Nov-05 . . . . . . . . 5-Jan-05  16716 2005  NO
9 1-Jul-05  31-Dec-05 . . . . . . . . 7-Jan-05  17332 2005  NO
10  1-Jun-04  30-Jun-05 . . . . . . . . 13-Jan-05 16827 2005  YES
;
run;

data long(drop=_: start: end:);
  set wide;
  array _start {*} start:;
  array _end {*} end:;
  attrib insurance_start_dt insurance_end_dt format=date9.;
  do _i=1 to dim(_start);
    if missing(_start[_i]) then continue;
    insurance_start_dt=_start[_i];
    insurance_end_dt=_end[_i];
    output;
  end;
run;

proc sql;
/*  create table want as*/
    select 
      *,
      case
        when 
          (select count(*) from long i where i.id=o.id and i.DATE_DIAGNOSIS between i.insurance_start_dt and i.insurance_end_dt) &amp;gt;0 then 'YES'
          else 'NO'
        end length=3 
        as KEEP_CORRECT2,
      case
        when 
          (select count(*) from long i where i.id=o.id and i.DATE_DIAGNOSIS between o.insurance_start_dt and o.insurance_end_dt) &amp;gt;0 then '1'
          else '0'
        end length=1 
        as match_flag
    from long o
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;N.B: A look-up implemented within a SQL Select clause is from a performance perspective not ideal and you will always have to find the right balance between performance and simple, maintainable code.&lt;/P&gt;</description>
      <pubDate>Sat, 06 Apr 2019 03:31:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-date-points-fall-in-the-time-intervals-for-eligibility/m-p/549010#M152288</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-04-06T03:31:52Z</dc:date>
    </item>
    <item>
      <title>Re: Check if date points fall in the time intervals for eligibility assessment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-date-points-fall-in-the-time-intervals-for-eligibility/m-p/549045#M152301</link>
      <description>&lt;P&gt;Using &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt; 's example data (your step as posted does not work because of tabs, see the log), tranpose and then it's a very simple data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose
  data=wide
  out=start (
    rename=(col1=start)
    where=(start ne .)
  )
;
by id;
var start:;
run;

data start1;
set start;
period = input(substr(_name_,6),best.);
drop _name_;
run;

proc transpose
  data=wide
  out=end (
    rename=(col1=end)
    where=(end ne .)
  )
;
by id;
var end:;
run;

data end1;
set end;
period = input(substr(_name_,4),best.);
drop _name_;
run;

data trans;
merge
  start1
  end1
;
by id period;
run;

data want;
merge
  wide (drop=start: end:)
  trans
;
by id;
if start &amp;lt;= date_diagnosis &amp;lt;= end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 06 Apr 2019 14:45:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-date-points-fall-in-the-time-intervals-for-eligibility/m-p/549045#M152301</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-04-06T14:45:15Z</dc:date>
    </item>
    <item>
      <title>Re: Check if date points fall in the time intervals for eligibility assessment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-date-points-fall-in-the-time-intervals-for-eligibility/m-p/549047#M152303</link>
      <description>Thanks a lot Kurt. I'm trying to understand that you said the code didn't work because of tabs?</description>
      <pubDate>Sat, 06 Apr 2019 14:53:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-date-points-fall-in-the-time-intervals-for-eligibility/m-p/549047#M152303</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2019-04-06T14:53:17Z</dc:date>
    </item>
    <item>
      <title>Re: Check if date points fall in the time intervals for eligibility assessment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-date-points-fall-in-the-time-intervals-for-eligibility/m-p/549048#M152304</link>
      <description>&lt;P&gt;The data eligibility step contains tabs in the datalines, and that's a delimiter that does not work if it is not set in an infile statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hint from me: disable the use of tabs in the Enhanced Editor, and also set it to convert tabs to blanks on saving and loading.&lt;/P&gt;</description>
      <pubDate>Sat, 06 Apr 2019 14:59:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-date-points-fall-in-the-time-intervals-for-eligibility/m-p/549048#M152304</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-04-06T14:59:36Z</dc:date>
    </item>
    <item>
      <title>Re: Check if date points fall in the time intervals for eligibility assessment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-date-points-fall-in-the-time-intervals-for-eligibility/m-p/549051#M152307</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt; Thanks a lot. What is match_flag here? do we need it?&lt;/P&gt;</description>
      <pubDate>Sat, 06 Apr 2019 15:40:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-date-points-fall-in-the-time-intervals-for-eligibility/m-p/549051#M152307</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2019-04-06T15:40:24Z</dc:date>
    </item>
    <item>
      <title>Re: Check if date points fall in the time intervals for eligibility assessment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-date-points-fall-in-the-time-intervals-for-eligibility/m-p/549055#M152308</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/132289"&gt;@Cruise&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt; Thanks a lot. What is match_flag here? do we need it?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I've just added match_flag to mark the row with the matching time period and also to demonstrate how a small change in the SQL logic will yield a different result. If you don't need this information then just remove the logic from the SQL.&lt;/P&gt;</description>
      <pubDate>Sat, 06 Apr 2019 19:37:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-date-points-fall-in-the-time-intervals-for-eligibility/m-p/549055#M152308</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-04-06T19:37:19Z</dc:date>
    </item>
    <item>
      <title>Re: Check if date points fall in the time intervals for eligibility assessment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-date-points-fall-in-the-time-intervals-for-eligibility/m-p/549064#M152310</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt; both approaches worked on my analytical data just so perfectly and solutions agreed. Thank you so much for your help. &lt;/P&gt;</description>
      <pubDate>Sat, 06 Apr 2019 21:26:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-date-points-fall-in-the-time-intervals-for-eligibility/m-p/549064#M152310</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2019-04-06T21:26:19Z</dc:date>
    </item>
  </channel>
</rss>

