<?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 on same variable date n key but different values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576870#M163357</link>
    <description>&lt;P&gt;Below code works for your sample data. You will have to verify if that's also an appropriate solution for your real data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data d1;
  infile cards dsd truncover;
  input id $ amount date:ddmmyy10.;
  format date ddmmyy10.;
  cards;
aa,50,06/06/2019
aa,50,07/06/2019
aa,20,08/06/2019
aa,40,09/06/2019
bb,10,03/06/2019
bb,20,04/06/2019
bb,30,08/06/2019
;
run;

data d2;
  infile cards dsd dlm= "," missover;
  input id $ amount date:ddmmyy10. curr $;
  cards;
aa,50,07/06/2019,gbp1
aa,50,08/06/2019,gbp2
aa,20,10/06/2019,gbp3
aa,40,09/06/2019,gbp4
aa,60,12/06/2019,gbp5
bb,10,02/06/2019,gbp6
bb,20,04/06/2019,gbp7
bb,30,08/06/2019,gbp8
;
run;

proc sql feedback;
/*  create table want as*/
    select h1.*, h2.curr
    from d1 h1 left join d2 h2
    on h1.id=h2.id and h1.amount=h2.amount and h1.date-h2.date between -1 and 1
    group by h1.id, h1.amount, h1.date
    having abs(h1.date-h2.date)=min(abs(h1.date-h2.date))
    order by h1.id, h1.date, h1.amount
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 193px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31327i089A6D5B9246BAB6/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you also want a match for row 3 then increase the range of dates included in the join by modifying below condition.&lt;/P&gt;
&lt;PRE&gt;h1.date-h2.date between -1 and 1&lt;/PRE&gt;
&lt;P&gt;I'd keep the date range as narrow as possible to avoid false positive matches.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 26 Jul 2019 11:24:50 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2019-07-26T11:24:50Z</dc:date>
    <item>
      <title>joining on same variable date n key but different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576837#M163342</link>
      <description>&lt;P&gt;Hi All ,&lt;/P&gt;&lt;P&gt;I have got two datasets as below&amp;nbsp;&lt;/P&gt;&lt;P&gt;dataset 1:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;FONT face="Arial" size="2"&gt;id&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;amount&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;date&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;aa&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;50&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;06/06/2019&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;aa&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;10&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;07/06/2019&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;aa&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;20&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;08/06/2019&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;aa&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;40&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;09/06/2019&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;bb&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;10&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;03/06/2019&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;bb&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;20&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;04/06/2019&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;bb&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;30&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;08/06/2019&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dataset 2&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;id&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;amount&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;date&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;curr&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;aa&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;50&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;07/06/2019&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;gbp&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;aa&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;10&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;08/06/2019&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;gbp&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;aa&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;20&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;10/06/2019&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;gbp&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;aa&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;40&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;09/06/2019&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;gbp&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;aa&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;60&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;12/06/2019&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;gbp&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;bb&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;10&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;02/06/2019&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;gbp&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;bb&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;20&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;04/06/2019&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;gbp&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;bb&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;30&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV align="right"&gt;&lt;FONT face="Arial" size="2"&gt;08/06/2019&lt;/FONT&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" size="2"&gt;gbp&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need all records from Datset1 basically a left join but when I am trying to join on Id and amount I am getting loads of dups&amp;nbsp;&lt;/P&gt;&lt;P&gt;amount matches to the date but both datasets have got different dates.Therefore when I am trying to match&amp;nbsp; them left join is making loads of dups.&lt;/P&gt;&lt;P&gt;I need an urgent help for this query .&lt;/P&gt;&lt;P&gt;Any any guidance will be much appreciated.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2019 08:48:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576837#M163342</guid>
      <dc:creator>MV04</dc:creator>
      <dc:date>2019-07-26T08:48:27Z</dc:date>
    </item>
    <item>
      <title>Re: joining on same variable date n key but different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576838#M163343</link>
      <description>&lt;P&gt;If using your sample data there wouldn't be duplicates when joining over ID and Amount so first thing you should be doing is post representative sample data which showcase the issue.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"&lt;EM&gt;amount matches to the date but both datasets have got different dates&lt;/EM&gt;"&lt;/P&gt;
&lt;P&gt;Is there any logic to how the dates for "matching" rows differ? Can you describe in word the rules how you would need to join so that you don't hit many to many conditions.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2019 09:08:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576838#M163343</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-07-26T09:08:11Z</dc:date>
    </item>
    <item>
      <title>Re: joining on same variable date n key but different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576840#M163344</link>
      <description>&lt;P&gt;Thanks for the response Patrik,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Both datasets are from different sources one may be running on weekdays and getting dates as the process day and if it is a weekend or a bank holiday then dates are skipping those&amp;nbsp; whereas other may be running on from tues- saturday something like that hence they are different so id and amount is same but for some records when ID and amount is same date is same as well whereas for others id and amont is same but date is different&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2019 09:15:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576840#M163344</guid>
      <dc:creator>MV04</dc:creator>
      <dc:date>2019-07-26T09:15:27Z</dc:date>
    </item>
    <item>
      <title>Re: joining on same variable date n key but different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576843#M163345</link>
      <description>also I was joining on id ,amount as a left join but getting loads of dups</description>
      <pubDate>Fri, 26 Jul 2019 09:16:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576843#M163345</guid>
      <dc:creator>MV04</dc:creator>
      <dc:date>2019-07-26T09:16:33Z</dc:date>
    </item>
    <item>
      <title>Re: joining on same variable date n key but different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576848#M163348</link>
      <description>&lt;P&gt;I am not sure the following answers you. If not, show the expected output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
   if _n_ = 1 then do;
      if 0 then set d2;
      declare hash h(dataset:'d2', ordered:'Y');
      h.definekey('id','amount');
      h.definedata('id','amount','date','curr');
      h.definedone();
   end;
   set d1;
   if h.find() = 0 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Jul 2019 09:40:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576848#M163348</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-26T09:40:11Z</dc:date>
    </item>
    <item>
      <title>Re: joining on same variable date n key but different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576849#M163349</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/269995"&gt;@MV04&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;also I was joining on id ,amount as a left join but getting loads of dups&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Not with the sample data you've posted - that's why I'm asking for better sample data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
  infile datalines truncover dsd;
  input id $ amount date :ddmmyy10.;
  datalines;
aa,50,6/06/2019,
aa,10,7/06/2019,
aa,20,8/06/2019,
aa,40,9/06/2019,
bb,10,3/06/2019,
bb,20,4/06/2019,
bb,30,8/06/2019,
;

data have2;
  infile datalines truncover dsd;
  input id $ amount date :ddmmyy10. curr $;
  datalines;
aa,50,7/06/2019,gbp
aa,10,8/06/2019,gbp
aa,20,10/06/2019,gbp
aa,40,9/06/2019,gbp
aa,60,12/06/2019,gbp
bb,10,2/06/2019,gbp
bb,20,4/06/2019,gbp
bb,30,8/06/2019,gbp
;

proc sql;
/*  create table want as*/
    select h1.*, h2.curr
    from have1 h1 left join have2 h2
    on h1.id=h2.id and h1.amount=h2.amount
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 163px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31326i9CC4205E96E999A6/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2019 09:40:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576849#M163349</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-07-26T09:40:19Z</dc:date>
    </item>
    <item>
      <title>Re: joining on same variable date n key but different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576850#M163350</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/269995"&gt;@MV04&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for the response Patrik,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Both datasets are from different sources one may be running on weekdays and getting dates as the process day and if it is a weekend or a bank holiday then dates are skipping those&amp;nbsp; whereas other may be running on from tues- saturday something like that hence they are different so id and amount is same but for some records when ID and amount is same date is same as well whereas for others id and amont is same but date is different&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You give a high level description of the problem here. Where you need to be at before even starting to write code is a description of the solution (=a description of the logic which allows to match records from the two sources without any ambiguity).&lt;/P&gt;
&lt;P&gt;From your description right now I'm not sure if such a join is possible but there might be other data elements in your tables which could resolve this. So how - just by "eyeballing" the data - would you decide if records with different dates belong to the same "transaction".&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also: Is "amount" a valid key at all? Are these end-of-day amounts - but then why could they be always the same on different dates if the cut-off date in the two systems is different. ....which gets me to the question: What are these dates you're using right now? Rundates, cutoff dates, something else? And would there be eventually other date columns in your data which match?&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2019 09:55:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576850#M163350</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-07-26T09:55:23Z</dc:date>
    </item>
    <item>
      <title>Re: joining on same variable date n key but different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576852#M163352</link>
      <description>&lt;P&gt;Hi Parick,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below are the dataset&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2" color="#000080"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;d1;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2" color="#0000FF"&gt;infile&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#0000FF"&gt;cards&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#0000FF"&gt;dsd&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#0000FF"&gt;truncover&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2" color="#0000FF"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;id $ amount date:&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#008080"&gt;ddmmyy10.&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2" color="#0000FF"&gt;cards&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2"&gt;aa,50,06/06/2019&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2"&gt;aa,50,07/06/2019&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2"&gt;aa,20,08/06/2019&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2"&gt;aa,40,09/06/2019&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2"&gt;bb,10,03/06/2019&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2"&gt;bb,20,04/06/2019&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2"&gt;bb,30,08/06/2019&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#000080"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2" color="#000080"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;d2;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2" color="#0000FF"&gt;infile&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#0000FF"&gt;cards&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#0000FF"&gt;dsd&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#0000FF"&gt;dlm&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#800080"&gt;","&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#0000FF"&gt;missover&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2" color="#0000FF"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;id $ amount date:&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#008080"&gt;ddmmyy10.&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;curr $;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2" color="#0000FF"&gt;cards&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2"&gt;aa,50,07/06/2019,gbp&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2"&gt;aa,50,08/06/2019,gbp&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2"&gt;aa,20,10/06/2019,gbp&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2"&gt;aa,40,09/06/2019,gbp&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2"&gt;aa,60,12/06/2019,gbp&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2"&gt;bb,10,02/06/2019,gbp&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2"&gt;bb,20,04/06/2019,gbp&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2"&gt;bb,30,08/06/2019,gbp&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#000080"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;FONT face="sans-serif" size="2"&gt;&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="sans-serif" size="2"&gt;and the output soule be&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="sans-serif" size="2"&gt;&lt;IMG src="https://dl-mail.ymail.com/ws/download/mailboxes/@.id==VjN-M1F0tGvOAgcX3B15N01wBmkvhPAUzich1RJqkVo5hMNUPlm8ySoLMqwcKX3kNg2oOmrGV-HH0bOkldH1UPfqOw/messages/@.id==AO5nN-lmO3GwXTrQ4QcQmGr58KY/content/parts/@.id==2/raw?appid=YMailNorrin&amp;amp;ymreqid=993eaf8e-9f42-9bb1-1c1f-c8000101c900&amp;amp;token=zitEzqOML3j84e6ealFTT5U7-km5qEQF52lp7AcCuBbrZMIhLGA5oNG194ZgDjbeSvifTR6B0Ee_hgUPpWAh9ah5y7-lGn8QFrsC5_stY-szo518we-cgmfUm53rqcmk" border="0" width="306" height="153" /&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2019 10:07:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576852#M163352</guid>
      <dc:creator>MV04</dc:creator>
      <dc:date>2019-07-26T10:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: joining on same variable date n key but different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576855#M163354</link>
      <description>Patrick I have sent a better data basicaly duplicate of amount on diff dates</description>
      <pubDate>Fri, 26 Jul 2019 10:25:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576855#M163354</guid>
      <dc:creator>MV04</dc:creator>
      <dc:date>2019-07-26T10:25:25Z</dc:date>
    </item>
    <item>
      <title>Re: joining on same variable date n key but different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576856#M163355</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/269995"&gt;@MV04&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looks like the picture for the expected result didn't make it. Can you please post it again.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also: I suggest you mock-up a different value for curr for every single row (like: gpb1, gbp2,...) so it becomes obvious in your expected result which row from d2 you'd be joining to d1.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2019 10:28:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576856#M163355</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-07-26T10:28:35Z</dc:date>
    </item>
    <item>
      <title>Re: joining on same variable date n key but different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576858#M163356</link>
      <description>&lt;P&gt;In the hash solution replace:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;h.definekey('id','amount');&lt;/PRE&gt;
&lt;P&gt;BY&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;h.definekey('id','date');&lt;/PRE&gt;
&lt;P&gt;See whether you get the required output.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2019 10:35:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576858#M163356</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-26T10:35:56Z</dc:date>
    </item>
    <item>
      <title>Re: joining on same variable date n key but different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576870#M163357</link>
      <description>&lt;P&gt;Below code works for your sample data. You will have to verify if that's also an appropriate solution for your real data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data d1;
  infile cards dsd truncover;
  input id $ amount date:ddmmyy10.;
  format date ddmmyy10.;
  cards;
aa,50,06/06/2019
aa,50,07/06/2019
aa,20,08/06/2019
aa,40,09/06/2019
bb,10,03/06/2019
bb,20,04/06/2019
bb,30,08/06/2019
;
run;

data d2;
  infile cards dsd dlm= "," missover;
  input id $ amount date:ddmmyy10. curr $;
  cards;
aa,50,07/06/2019,gbp1
aa,50,08/06/2019,gbp2
aa,20,10/06/2019,gbp3
aa,40,09/06/2019,gbp4
aa,60,12/06/2019,gbp5
bb,10,02/06/2019,gbp6
bb,20,04/06/2019,gbp7
bb,30,08/06/2019,gbp8
;
run;

proc sql feedback;
/*  create table want as*/
    select h1.*, h2.curr
    from d1 h1 left join d2 h2
    on h1.id=h2.id and h1.amount=h2.amount and h1.date-h2.date between -1 and 1
    group by h1.id, h1.amount, h1.date
    having abs(h1.date-h2.date)=min(abs(h1.date-h2.date))
    order by h1.id, h1.date, h1.amount
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 193px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31327i089A6D5B9246BAB6/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you also want a match for row 3 then increase the range of dates included in the join by modifying below condition.&lt;/P&gt;
&lt;PRE&gt;h1.date-h2.date between -1 and 1&lt;/PRE&gt;
&lt;P&gt;I'd keep the date range as narrow as possible to avoid false positive matches.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2019 11:24:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576870#M163357</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-07-26T11:24:50Z</dc:date>
    </item>
    <item>
      <title>Re: joining on same variable date n key but different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576872#M163358</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17813"&gt;@KachiM&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe you've missed that the dates can differ. If going for a hash solution you probably would need to load the hash with keys {id,amount} and multidata:'y' and then use the do_over() method to loop over a tuple and find the item where the dates between hash and base table differ the least.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2019 11:33:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576872#M163358</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-07-26T11:33:20Z</dc:date>
    </item>
    <item>
      <title>Re: joining on same variable date n key but different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576899#M163370</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for pointing to my misunderstanding of the OPs specification. Hope your solution works for the OP.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2019 13:33:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576899#M163370</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-26T13:33:03Z</dc:date>
    </item>
    <item>
      <title>Re: joining on same variable date n key but different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576901#M163372</link>
      <description>how do increase the range</description>
      <pubDate>Fri, 26 Jul 2019 13:41:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576901#M163372</guid>
      <dc:creator>MV04</dc:creator>
      <dc:date>2019-07-26T13:41:22Z</dc:date>
    </item>
    <item>
      <title>Re: joining on same variable date n key but different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576912#M163374</link>
      <description>&lt;P&gt;Note questions raised by Patrick.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When ID and Amount is the Key for the search, why are you worried about the difference in Dates as you seem to compare only TWO sources. Please confirm whether it is only two sources.&amp;nbsp; If there are more than two sources, then add a column to the Sample Data set for the sourceID. Then the solution becomes easy with the Key having ID, Amount and SourceID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2019 14:27:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576912#M163374</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-26T14:27:11Z</dc:date>
    </item>
    <item>
      <title>Re: joining on same variable date n key but different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576972#M163404</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/269995"&gt;@MV04&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;how do increase the range&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Below bit in the ON clause determines which dates get selected for joining. So here the dates from the two tables may only differ by 1 day.&lt;/P&gt;
&lt;PRE&gt;h1.date-h2.date between -1 and 1&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;If you want to allow for 2 days then the condition would need to be:&lt;/P&gt;
&lt;PRE&gt;h1.date-h2.date between -2 and 2&lt;/PRE&gt;
&lt;P&gt;The HAVING clause then filters the result set after the join and selects the row where the difference between the two dates is minimal.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;having abs(h1.date-h2.date)=min(abs(h1.date-h2.date))&lt;/PRE&gt;
&lt;P&gt;Now.... That works with your sample data BUT if there is a case where in table D2 there isn't a date which matches exactly to D1 but there is a match both a day earlier and a day later then you still end up with duplicates. Question is: Which record in D2 should get selected logically? The earlier or the later one?&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2019 16:20:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/joining-on-same-variable-date-n-key-but-different-values/m-p/576972#M163404</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-07-26T16:20:55Z</dc:date>
    </item>
  </channel>
</rss>

