<?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: Joining tables with dates that have a dependence relation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Joining-tables-with-dates-that-have-a-dependence-relation/m-p/845475#M334251</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp; and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Thank you both for helping me with your wonderful codes. I tried and studied both (this is the reason I didn't answer immediately) as they give different results on my data. Although none of them gives the exact result I was looking for, the code of&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp; is that who is nearer. This is the reason why I choose your answer. I think I will write you a private message to ask you some explanation about your code&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;.&lt;/P&gt;
&lt;P&gt;Anyway, Thank you very much to both of you!&lt;/P&gt;</description>
    <pubDate>Mon, 21 Nov 2022 14:51:01 GMT</pubDate>
    <dc:creator>Haemoglobin17</dc:creator>
    <dc:date>2022-11-21T14:51:01Z</dc:date>
    <item>
      <title>Joining tables with dates that have a dependence relation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Joining-tables-with-dates-that-have-a-dependence-relation/m-p/843405#M333443</link>
      <description>&lt;P&gt;Hi everyone!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am joining two tables that have a variable id in common and each has a variable date (Date and Date2) that are causally related. In particular, Date can determine zero, one or more events in Date2. Here you can see an image of the two tables:&lt;/P&gt;
&lt;P&gt;Table1&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Capture 1.PNG" style="width: 401px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77122i42A0F9E352AAE921/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture 1.PNG" alt="Capture 1.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table2:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture 2.PNG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77125iCB11201F2164A224/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture 2.PNG" alt="Capture 2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I would like to join these two tables to show, for every event in Date if it is followed by 0,1 or more events of Date2.&amp;nbsp;Clearly every event in Date2 could be attributed to the oldest date, but I would like that every Date2 is attributed to a Date immediately before until there is another successive Date to attribute the Dates2 exactly as, if someone have an allergic reaction, it is more likely that this event is related to what he ate yesterday then what he ate six months before.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture 3.PNG" style="width: 644px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77127iEE5B2375EEFEA570/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture 3.PNG" alt="Capture 3.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also link some fake data as a working data set.&lt;/P&gt;
&lt;P&gt;data Database1;&lt;BR /&gt;input id $ date :ddmmyy10. ;&lt;BR /&gt;cards;&lt;BR /&gt;111 01/01/2015 &lt;BR /&gt;111 20/03/2015 &lt;BR /&gt;111 15/06/2016 &lt;BR /&gt;111 12/03/2017 &lt;BR /&gt;111 30/06/2020 &lt;BR /&gt;222 18/03/2020 &lt;BR /&gt;222 25/04/2020 &lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data Database2;&lt;BR /&gt;input id $ date2 :ddmmyy10. ;&lt;BR /&gt;cards;&lt;BR /&gt;111 18/06/2016&lt;BR /&gt;111 15/08/2016&lt;BR /&gt;111 17/03/2017&lt;BR /&gt;111 20/01/2018&lt;BR /&gt;111 04/04/2018&lt;BR /&gt;111 20/04/2020&lt;BR /&gt;222 26/05/2020&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much!&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2022 16:20:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Joining-tables-with-dates-that-have-a-dependence-relation/m-p/843405#M333443</guid>
      <dc:creator>Haemoglobin17</dc:creator>
      <dc:date>2022-11-09T16:20:43Z</dc:date>
    </item>
    <item>
      <title>Re: Joining tables with dates that have a dependence relation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Joining-tables-with-dates-that-have-a-dependence-relation/m-p/843506#M333475</link>
      <description>&lt;P&gt;Use next tested code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Database1;
   input id $ date :ddmmyy10. ;
cards;
111 01/01/2015
111 20/03/2015
111 15/06/2016
111 12/03/2017
111 30/06/2020
222 18/03/2020
222 25/04/2020
;
run;

data Database2;
   input id $ date2 :ddmmyy10. ;
cards;
111 18/06/2016
111 15/08/2016
111 17/03/2017
111 20/01/2018
111 04/04/2018
111 20/04/2020
222 26/05/2020
;
run;

data temp / view=temp ;
 set Database1 Database2;
     sortdt = coalesce(date, date2);
     format date date2 sortdt ddmmyy10.;
run;
proc sort data=temp out=temp1; by id sortdt ; run;

data want;
set temp1;
 by id sortdt ;
    retain dt1;
    if first.id then dt1=date; 
    if date then do;
       dt1=date; 
    end;
    else date=dt1;
    drop sortdt dt1;
run;
data want;
 set want;
  by id date;
     if not (first.date and last.date)
        and date2=. then delete;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Nov 2022 05:35:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Joining-tables-with-dates-that-have-a-dependence-relation/m-p/843506#M333475</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2022-11-10T05:35:11Z</dc:date>
    </item>
    <item>
      <title>Re: Joining tables with dates that have a dependence relation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Joining-tables-with-dates-that-have-a-dependence-relation/m-p/843551#M333486</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
input id $ date :ddmmyy10.;
format date ddmmyy10.;
cards;
111 01/01/2015
111 20/03/2015
111 15/06/2016
111 12/03/2017
111 30/06/2020
222 18/03/2020
222 25/04/2020
;

data have2;
input id $ date2 :ddmmyy10.;
format date2 ddmmyy10.;
cards;
111 18/06/2016
111 15/08/2016
111 17/03/2017
111 20/01/2018
111 04/04/2018
111 20/04/2020
222 26/05/2020
;

data want(drop = d i);
   
   if _N_ = 1 then do;
      dcl hash h(dataset : 'have2', multidata : 'Y', ordered : 'Y');
      h.definekey('id');
      h.definedata('date2');
      h.definedone();
   end;

   set have1;
   by id; 
   set have1(keep = date rename=(date = d) firstobs = 2) 
       have1(drop = _all_ obs = 1);

   if last.id then d = '31dec9999'd;

   do i = 1 by 1 while (h.do_over() = 0);
      if date &amp;lt; date2 &amp;lt; d then output;

      else if i = 1 and (d = '31dec9999'd | date2 &amp;gt; d) then do;
         date2 = .;
         output;
      end;
   end;

   format date2 ddmmyy10.;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;id   date        date2
111  01/01/2015  .
111  20/03/2015  .
111  15/06/2016  18/06/2016
111  15/06/2016  15/08/2016
111  12/03/2017  17/03/2017
111  12/03/2017  20/01/2018
111  12/03/2017  04/04/2018
111  12/03/2017  20/04/2020
111  30/06/2020  .
222  18/03/2020  .
222  25/04/2020  26/05/2020&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Nov 2022 13:34:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Joining-tables-with-dates-that-have-a-dependence-relation/m-p/843551#M333486</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-11-10T13:34:53Z</dc:date>
    </item>
    <item>
      <title>Re: Joining tables with dates that have a dependence relation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Joining-tables-with-dates-that-have-a-dependence-relation/m-p/845475#M334251</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp; and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Thank you both for helping me with your wonderful codes. I tried and studied both (this is the reason I didn't answer immediately) as they give different results on my data. Although none of them gives the exact result I was looking for, the code of&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp; is that who is nearer. This is the reason why I choose your answer. I think I will write you a private message to ask you some explanation about your code&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;.&lt;/P&gt;
&lt;P&gt;Anyway, Thank you very much to both of you!&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2022 14:51:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Joining-tables-with-dates-that-have-a-dependence-relation/m-p/845475#M334251</guid>
      <dc:creator>Haemoglobin17</dc:creator>
      <dc:date>2022-11-21T14:51:01Z</dc:date>
    </item>
  </channel>
</rss>

