<?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: Join records based on next closest date. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Join-records-based-on-next-closest-date/m-p/649230#M194610</link>
    <description>&lt;P&gt;Is there any rule involved for if two of the "other" dates have the same difference from the base date?&lt;/P&gt;
&lt;P&gt;Or if 3 or more records? ( possible if non-unique values of company identification or matching characteristic in the second set for some reason)&lt;/P&gt;</description>
    <pubDate>Wed, 20 May 2020 14:14:44 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-05-20T14:14:44Z</dc:date>
    <item>
      <title>Join records based on next closest date.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-records-based-on-next-closest-date/m-p/649177#M194597</link>
      <description>&lt;P&gt;I have one table that shows company announcements. These could be any date at random.&lt;/P&gt;
&lt;P&gt;I have another table that shows many different financials about the company from specific dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way to add a clause to the join where it will join the first table to the second by joining on the next closest date in the 2nd table?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, lets assume my announcement date was 5/5/2020.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lets assume that in the next table I had dates of 3/3/2020, 4/27/2020, 5/27/2020, and 6/4/2020.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to join these tables on the company name (which is both tables), but I want the join to keep the data from the 5/5/2020 announcement and all the data from the 5/27/2020 record (since its the next date).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 20 May 2020 11:38:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-records-based-on-next-closest-date/m-p/649177#M194597</guid>
      <dc:creator>anweinbe</dc:creator>
      <dc:date>2020-05-20T11:38:55Z</dc:date>
    </item>
    <item>
      <title>Re: Join records based on next closest date.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-records-based-on-next-closest-date/m-p/649185#M194599</link>
      <description>&lt;P&gt;With SQL only you would first join the two tables over company and then filter the intermediary result to pick the row where the difference between the two dates is minimal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;First join&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;select t1.cust_id, t1.date, t2.date&lt;/P&gt;
&lt;P&gt;join table1 t1 left join table2 t2&lt;/P&gt;
&lt;P&gt;on t1.cust_id=t2.cust_id&amp;nbsp; &amp;nbsp;...may be here also something like: and t2.date between t1.date-20 and t1.date+20 to not select all matching rows from t2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Then filter&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;group by t1.cust_id&lt;/P&gt;
&lt;P&gt;having abs(t1.date-t2.date)=min(abs(t1.date-t2.date))&lt;/P&gt;
&lt;P&gt;....now you could still end-up with two rows for you customer if there are two rows in t2 where you have dates which are exactly the same amount of days before or after the t1 date. So you will have to extend the filter to pick either the t2.date before or after the t1.date for such a case.&lt;/P&gt;</description>
      <pubDate>Wed, 20 May 2020 12:11:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-records-based-on-next-closest-date/m-p/649185#M194599</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-05-20T12:11:14Z</dc:date>
    </item>
    <item>
      <title>Re: Join records based on next closest date.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-records-based-on-next-closest-date/m-p/649194#M194602</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want (drop=dif) as
  select
    a.company,
    a.somedate,
    b.otherdate,
    abs(a.somedate - b.otherdate) as dif
  from have1 a, have2 b
  where a.company = b.company
  group by a.company, a.somedate
  having dif = min(dif)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(posted from my tablet, so untested)&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 May 2020 12:28:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-records-based-on-next-closest-date/m-p/649194#M194602</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-05-20T12:28:41Z</dc:date>
    </item>
    <item>
      <title>Re: Join records based on next closest date.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-records-based-on-next-closest-date/m-p/649230#M194610</link>
      <description>&lt;P&gt;Is there any rule involved for if two of the "other" dates have the same difference from the base date?&lt;/P&gt;
&lt;P&gt;Or if 3 or more records? ( possible if non-unique values of company identification or matching characteristic in the second set for some reason)&lt;/P&gt;</description>
      <pubDate>Wed, 20 May 2020 14:14:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-records-based-on-next-closest-date/m-p/649230#M194610</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-20T14:14:44Z</dc:date>
    </item>
    <item>
      <title>Re: Join records based on next closest date.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-records-based-on-next-closest-date/m-p/649278#M194623</link>
      <description>&lt;P&gt;Kurt,&lt;/P&gt;
&lt;P&gt;This gets me very very close to where I need to be.&lt;/P&gt;
&lt;P&gt;The issue I am running into is that in the second file there appears to be duplicate dates, so each of the items in my original table ends up matching to the date in the 2nd table 4-5 times each.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you suggest that I just take the resulting table from your code and then just remove duplicates on the resulting file?&lt;/P&gt;</description>
      <pubDate>Wed, 20 May 2020 16:14:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-records-based-on-next-closest-date/m-p/649278#M194623</guid>
      <dc:creator>anweinbe</dc:creator>
      <dc:date>2020-05-20T16:14:46Z</dc:date>
    </item>
    <item>
      <title>Re: Join records based on next closest date.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-records-based-on-next-closest-date/m-p/649291#M194630</link>
      <description>&lt;P&gt;If you only need one observation per company/somedate/otherdate, add the DISTINCT keyword to the SELECT.&lt;/P&gt;</description>
      <pubDate>Wed, 20 May 2020 16:37:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-records-based-on-next-closest-date/m-p/649291#M194630</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-05-20T16:37:13Z</dc:date>
    </item>
  </channel>
</rss>

