<?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 Date logic in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Date-logic/m-p/589336#M168548</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 2 tables as shown. Table 2 has duplicates. While joining the tables using left join, we want to pull from table 2 any dates &amp;gt;= the date for the ID from table T1 as well as dates within 7 days prior. Can someone help with the logic?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;TABLE T1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp;&amp;nbsp; dischargeDT&lt;/P&gt;&lt;P&gt;101&amp;nbsp; 08/01/2019&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;TABLE T2&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp;&amp;nbsp;&amp;nbsp; DATE&lt;/P&gt;&lt;P&gt;101&amp;nbsp; 06/16/2019&lt;/P&gt;&lt;P&gt;101&amp;nbsp; 07/25/2019&lt;/P&gt;&lt;P&gt;101&amp;nbsp; 08/01/2019&lt;/P&gt;&lt;P&gt;101&amp;nbsp; 09/05/2019&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;RESULT&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp;&amp;nbsp; discharged&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DATE&lt;/P&gt;&lt;P&gt;101&amp;nbsp; 08/01/2019&amp;nbsp;&amp;nbsp; 07/25/2019 ---7 days prior to 08/01/2019&lt;/P&gt;&lt;P&gt;101&amp;nbsp; 08/01/2019&amp;nbsp;&amp;nbsp; 08/01/2019&amp;nbsp;--on the same day as 08/01/2019&lt;/P&gt;&lt;P&gt;101&amp;nbsp; 08/01/2019&amp;nbsp;&amp;nbsp; 09/05/2019--anything after&amp;nbsp;08/01/2019&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 17 Sep 2019 13:06:57 GMT</pubDate>
    <dc:creator>robertrao</dc:creator>
    <dc:date>2019-09-17T13:06:57Z</dc:date>
    <item>
      <title>Date logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-logic/m-p/589336#M168548</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 2 tables as shown. Table 2 has duplicates. While joining the tables using left join, we want to pull from table 2 any dates &amp;gt;= the date for the ID from table T1 as well as dates within 7 days prior. Can someone help with the logic?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;TABLE T1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp;&amp;nbsp; dischargeDT&lt;/P&gt;&lt;P&gt;101&amp;nbsp; 08/01/2019&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;TABLE T2&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp;&amp;nbsp;&amp;nbsp; DATE&lt;/P&gt;&lt;P&gt;101&amp;nbsp; 06/16/2019&lt;/P&gt;&lt;P&gt;101&amp;nbsp; 07/25/2019&lt;/P&gt;&lt;P&gt;101&amp;nbsp; 08/01/2019&lt;/P&gt;&lt;P&gt;101&amp;nbsp; 09/05/2019&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;RESULT&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp;&amp;nbsp; discharged&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DATE&lt;/P&gt;&lt;P&gt;101&amp;nbsp; 08/01/2019&amp;nbsp;&amp;nbsp; 07/25/2019 ---7 days prior to 08/01/2019&lt;/P&gt;&lt;P&gt;101&amp;nbsp; 08/01/2019&amp;nbsp;&amp;nbsp; 08/01/2019&amp;nbsp;--on the same day as 08/01/2019&lt;/P&gt;&lt;P&gt;101&amp;nbsp; 08/01/2019&amp;nbsp;&amp;nbsp; 09/05/2019--anything after&amp;nbsp;08/01/2019&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 13:06:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-logic/m-p/589336#M168548</guid>
      <dc:creator>robertrao</dc:creator>
      <dc:date>2019-09-17T13:06:57Z</dc:date>
    </item>
    <item>
      <title>Re: Date logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-logic/m-p/589339#M168551</link>
      <description>&lt;P&gt;Is this what you are after?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;on t1.id=t2.id and date&amp;gt;=intnx('days',dischargeDT,-7);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data T1;
input ID   dischargeDT :mmddyy10.;
format dischargeDT mmddyy10.;
cards;
101  08/01/2019
;
 

data T2;
input ID    DATE :mmddyy10.;
format date mmddyy10.;
cards;
101  06/16/2019
101  07/25/2019
101  08/01/2019
101  09/05/2019
;

proc sql;
create table want as
select t1.*,date
from t1  left join t2 
on t1.id=t2.id and date&amp;gt;=intnx('days',dischargeDT,-7);
quit;

 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Sep 2019 13:19:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-logic/m-p/589339#M168551</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-17T13:19:17Z</dc:date>
    </item>
    <item>
      <title>Re: Date logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-logic/m-p/589340#M168552</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data t1;
input id dischargedt :mmddyy10.;
format dischargedt mmddyy10.;
datalines;
101 08/01/2019
;

data t2;
input id date :mmddyy10.;
format date mmddyy10.;
datalines;
101 06/16/2019
101 07/25/2019
101 08/01/2019
101 09/05/2019
;

proc sql;
    create table want as
    select t1.*, t2.date
    from t1, t2
    where t2.date ge intnx('day', t1.dischargedt, -7, 's')
    group by t2.id
    order by t2.id, date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;id  dischargedt  date
101	08/01/2019	 07/25/2019
101	08/01/2019	 08/01/2019
101	08/01/2019	 09/05/2019&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Sep 2019 13:15:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-logic/m-p/589340#M168552</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-09-17T13:15:08Z</dc:date>
    </item>
    <item>
      <title>Re: Date logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-logic/m-p/589342#M168554</link>
      <description>&lt;P&gt;Please try&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data T1;
input ID   dischargeDT:mmddyy10.;
format dischargeDT date9.;
cards;
101  08/01/2019
;

data T2;
input ID    DATE:mmddyy10.;
format DATE date9.;
cards;
101  06/16/2019
101  07/25/2019
101  08/01/2019
101  09/05/2019
;

proc sql;
create table want as select a.*,b.dischargeDT  from t2 as a right join t1 as b on a.id=b.id and a.date&amp;gt;=b.dischargeDT or (a.date)&amp;gt;=(b.dischargeDT-7);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 13:21:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-logic/m-p/589342#M168554</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-09-17T13:21:51Z</dc:date>
    </item>
    <item>
      <title>Re: Date logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-logic/m-p/589487#M168616</link>
      <description>&lt;P&gt;Here is a solution much faster than SQL if (1) your data are sorted by ID and (2) you have only 1 obs per ID in T1:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE style="width: 816px; height: 538px;"&gt;&lt;CODE class=" language-sas"&gt;data t1;
  input id dischargedt :mmddyy10. ;
  format dischargedt date9.;
datalines;
101 08/01/2019
run;

data t2;
  input id  date :mmddyy10.;
  format date date9.;
datalines;
101  06/16/2019
101  07/25/2019
101  08/01/2019
101  09/05/2019
run;

data want;
  set t1 (keep=id in=in1) t2 (keep=id in=in2);
  by id;
  if in1 then set t1;
  if in2 then set t2;&lt;BR /&gt;
  if nmiss(dischargedt,date)=0;
  if date&amp;gt;=dischargedt-7 then output;
  if last.id then call missing(of _all_);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first SET statement does nothing but interleave a sequence of identical ID values (and track their sources by menas of IN1 and IN2).&amp;nbsp; Since T1 is the first object of the SET statement it provides the first instance of the set of identiflcal ID's.&amp;nbsp;&amp;nbsp; When that happens, then IN1=1&amp;nbsp; and the "if in1 then set t1" statement reads in all the remaining T1 variables - namely dischargedt, &lt;EM&gt;&lt;STRONG&gt;which will not be replaced until the next instance of IN1=1&lt;/STRONG&gt;&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When IN2=1, then a regular DATE value is read from T2.&amp;nbsp; It is compared to dischargedt-7 and output if it is no earlier.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now since variables like DISCHARGEDT and DATE are "retained" until the next corresponding SET statement is executed, it is possible that the dischargedt for ID 101 could contaminate T2 data for ID 102 (if there is no T1 data for 102).&amp;nbsp; That is why there is a "call missing (of _all_)" when each ID is exhausted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;
&lt;P&gt;Mark&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 21:19:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-logic/m-p/589487#M168616</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-09-17T21:19:25Z</dc:date>
    </item>
    <item>
      <title>Re: Date logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-logic/m-p/589488#M168617</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp; &amp;nbsp;Beautiful algorithm. More importantly, Thank you for the notes!&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 21:28:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-logic/m-p/589488#M168617</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-17T21:28:19Z</dc:date>
    </item>
  </channel>
</rss>

