<?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 join two tables by comparing date in range dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/join-two-tables-by-comparing-date-in-range-dates/m-p/802600#M315971</link>
    <description>&lt;PRE&gt;
Data have_contract ;
input contract_number contract_version version_begin_date ;
informat version_begin_date ddmmyy10. ;
format version_begin_date ddmmyy10. ;
datalines ;
123456789 1 01/09/2021
123456789 2 02/10/2021
123456789 3 03/11/2021
run ;

data have_event ;
input contract_number event_date ;
informat event_date ddmmyy10. ;
format event_date ddmmyy10. ;
datalines;
123456789 01/09/2021
123456789 08/09/2021
123456789 03/10/2021
123456789 30/10/2021
123456789 03/11/2021
123456789 13/11/2021
123456789 23/11/2021
run ;


Data want ;
input contract_number contract_version version_begin_date event_date ;
informat version_begin_date event_date ddmmyy10. ;
format version_begin_date event_date ddmmyy10. ;
datalines ;
123456789 1 01/09/2021 01/09/2021
123456789 1 01/09/2021 08/09/2021
123456789 2 02/10/2021 03/10/2021
123456789 2 02/10/2021 30/10/2021
123456789 3 03/11/2021 03/11/2021
123456789 3 03/11/2021 13/11/2021
123456789 3 03/11/2021 23/11/2021
run ;&lt;/PRE&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I would like to join this 2 tables "have".&lt;/P&gt;
&lt;P&gt;But I only want to join the events wich have an event date greater or equal the begin_date of the version and less than the the begin date of the next version.&lt;/P&gt;
&lt;P&gt;thanks in advance for your help&lt;/P&gt;
&lt;P&gt;kind regards&lt;/P&gt;
&lt;P&gt;Nasser&lt;/P&gt;</description>
    <pubDate>Thu, 17 Mar 2022 09:48:09 GMT</pubDate>
    <dc:creator>Nasser_DRMCP</dc:creator>
    <dc:date>2022-03-17T09:48:09Z</dc:date>
    <item>
      <title>join two tables by comparing date in range dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/join-two-tables-by-comparing-date-in-range-dates/m-p/802600#M315971</link>
      <description>&lt;PRE&gt;
Data have_contract ;
input contract_number contract_version version_begin_date ;
informat version_begin_date ddmmyy10. ;
format version_begin_date ddmmyy10. ;
datalines ;
123456789 1 01/09/2021
123456789 2 02/10/2021
123456789 3 03/11/2021
run ;

data have_event ;
input contract_number event_date ;
informat event_date ddmmyy10. ;
format event_date ddmmyy10. ;
datalines;
123456789 01/09/2021
123456789 08/09/2021
123456789 03/10/2021
123456789 30/10/2021
123456789 03/11/2021
123456789 13/11/2021
123456789 23/11/2021
run ;


Data want ;
input contract_number contract_version version_begin_date event_date ;
informat version_begin_date event_date ddmmyy10. ;
format version_begin_date event_date ddmmyy10. ;
datalines ;
123456789 1 01/09/2021 01/09/2021
123456789 1 01/09/2021 08/09/2021
123456789 2 02/10/2021 03/10/2021
123456789 2 02/10/2021 30/10/2021
123456789 3 03/11/2021 03/11/2021
123456789 3 03/11/2021 13/11/2021
123456789 3 03/11/2021 23/11/2021
run ;&lt;/PRE&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I would like to join this 2 tables "have".&lt;/P&gt;
&lt;P&gt;But I only want to join the events wich have an event date greater or equal the begin_date of the version and less than the the begin date of the next version.&lt;/P&gt;
&lt;P&gt;thanks in advance for your help&lt;/P&gt;
&lt;P&gt;kind regards&lt;/P&gt;
&lt;P&gt;Nasser&lt;/P&gt;</description>
      <pubDate>Thu, 17 Mar 2022 09:48:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/join-two-tables-by-comparing-date-in-range-dates/m-p/802600#M315971</guid>
      <dc:creator>Nasser_DRMCP</dc:creator>
      <dc:date>2022-03-17T09:48:09Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables by comparing date in range dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/join-two-tables-by-comparing-date-in-range-dates/m-p/802616#M315980</link>
      <description>&lt;P&gt;hello&lt;/P&gt;
&lt;P&gt;I succeeded to get the result I expected . first I sort my data by descending date .and then with LAG I could determine the version_begin_date_next.&lt;/P&gt;
&lt;P&gt;and then with the filter below&lt;/P&gt;
&lt;P&gt;and event_date&amp;gt;= a.version_begin_date&lt;BR /&gt;and b.event_date &amp;lt; a.version_bvegin_date_next&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nasser&lt;/P&gt;</description>
      <pubDate>Thu, 17 Mar 2022 12:15:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/join-two-tables-by-comparing-date-in-range-dates/m-p/802616#M315980</guid>
      <dc:creator>Nasser_DRMCP</dc:creator>
      <dc:date>2022-03-17T12:15:22Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables by comparing date in range dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/join-two-tables-by-comparing-date-in-range-dates/m-p/802635#M315989</link>
      <description>&lt;P&gt;This is a case of "bringing history forward".&amp;nbsp; I.e. you want to bring forward ("retain" in SAS language) the contract record through all subsequent event records, until the next contract record.&amp;nbsp; And you want one record per event.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For sorted data it's easy:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have_contract (in=inc keep=contract_number version_begin_date rename=(version_begin_date=event_date))
      have_event    (in=ine); 
  by contract_number event_date;
  if inc then set have_contract;
  if ine;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note this program depends a little on the data being relatively "clean".&amp;nbsp; That is, it assumes that no contract has an (erroneous) event date that precedes the first contract date.&amp;nbsp; If such a condition exists, then that event would inherit data from the preceding contract.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "trick" here, is that all the variables that are only read in by the "if inc then set have_contract" are retained through all subsequent event records until the next have_contract date.&amp;nbsp; &amp;nbsp;This in turn is why the first SET statement only keeps the two BY-variables from have_contract, thereby avoiding overwriting all the other have_contract variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Editted note: Yes, this can also be done via PROC SQL, but would likely require a good deal more code to satisfy an atavistic impulse.&amp;nbsp;&amp;nbsp;And this technique is easily expanded to apply to more than two input datasets.&amp;nbsp; See&amp;nbsp;&lt;A href="https://www.lexjansen.com/pharmasug/2019/BP/PharmaSUG-2019-BP-302.pdf" target="_self"&gt;History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies&lt;/A&gt;.&amp;nbsp;&amp;nbsp;&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;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/147725"&gt;@Nasser_DRMCP&lt;/a&gt;&amp;nbsp;asked:&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;is it possible to get all contracts even thoses without events ?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Yes.&amp;nbsp; Drop the&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if ine:&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;statement.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Mar 2022 02:46:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/join-two-tables-by-comparing-date-in-range-dates/m-p/802635#M315989</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-03-30T02:46:00Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables by comparing date in range dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/join-two-tables-by-comparing-date-in-range-dates/m-p/804789#M316967</link>
      <description>&lt;P&gt;Hi mkeintz&lt;/P&gt;
&lt;P&gt;many thanks for your help. I have an additional question.&lt;/P&gt;
&lt;P&gt;I notice that if a contratc exists in the in have_contract table but not in the have_event table then this contract does not exist in the "want" table.&lt;/P&gt;
&lt;P&gt;is it possible to get all contracts even thoses without events ?&lt;/P&gt;
&lt;P&gt;thanks in advance&lt;/P&gt;
&lt;P&gt;Nasser&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2022 15:21:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/join-two-tables-by-comparing-date-in-range-dates/m-p/804789#M316967</guid>
      <dc:creator>Nasser_DRMCP</dc:creator>
      <dc:date>2022-03-29T15:21:25Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables by comparing date in range dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/join-two-tables-by-comparing-date-in-range-dates/m-p/804901#M316999</link>
      <description>&lt;P&gt;/*First sort the have_contract and create a lag variable:*/&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have_Contract; by descending version_begin_date; run;&lt;/P&gt;
&lt;P&gt;data have_contract2;&lt;BR /&gt;set have_contract;&lt;BR /&gt;version_lag = lag(version_begin_date);&lt;BR /&gt;format version_lag ddmmyy10.;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/*Then create a Cartesian product followed by filtering for what you need:*/&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table all1 as select a.*, b.event_date from have_contract2 a, have_event b where a.contract_number=b.contract_number&lt;BR /&gt;order by version_begin_date;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data all2;&lt;BR /&gt;set all1;&lt;BR /&gt;if version_lag ne . and version_begin_date=&amp;lt;event_date&amp;lt;version_lag then inc = 1; &lt;BR /&gt;else if version_lag eq . and version_begin_date=&amp;lt;event_date then inc =1; &lt;BR /&gt;if inc=1;&lt;BR /&gt;drop version_lag inc;&lt;BR /&gt;run;&lt;BR /&gt;proc sort data=all2; by contract_number contract_version version_begin_date event_date;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2022 21:51:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/join-two-tables-by-comparing-date-in-range-dates/m-p/804901#M316999</guid>
      <dc:creator>Tommer</dc:creator>
      <dc:date>2022-03-29T21:51:37Z</dc:date>
    </item>
  </channel>
</rss>

