<?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: Left join a date to row with max date where duplicate records exist in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/683871#M207174</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/248692"&gt;@TheNovice&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Apologies, yes, the join key would be ban&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;According to above I have added BAN to table b.&lt;/P&gt;
&lt;P&gt;Next code is tested:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table_a;
 input ban col_date :date9.;
 format col_date date9.;
cards;
123 20-jun-20
123 21-jul-20
; run;

data table_b;
 input ban status $ status_date :date9.;
 format status_date date9.;
cards;
123 r 20-aug-20
; run;

proc sql;
 create table want(drop=dif_date min_date) as
 select a.ban, a.col_date,
        b.status_date - a.col_date as dif_date,
        min(calculated dif_date) as min_date,
        case when calculated dif_date = calculated min_date
             then b.status 
             else  ' ' end as status ,
        case when calculated dif_date = calculated min_date
             then b.status_date 
             else  .   end as status_date format=date9.
 from table_a as a 
 left join table_b as b 
 on a.ban = b.ban 
 group by a.ban 
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 15 Sep 2020 06:41:17 GMT</pubDate>
    <dc:creator>Shmuel</dc:creator>
    <dc:date>2020-09-15T06:41:17Z</dc:date>
    <item>
      <title>Left join a date to row with max date where duplicate records exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/683840#M207158</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I have to solve this using proc sql because it will then be converted to a sql script for automation and needs to be as close as possible (just how things work here).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;My dataset has duplicate records like below with different dates. I need to join to a second table where b.date &amp;gt; a.date. But i only want to join to the closest match to b.date which is the max (a.date)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Table a&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;BAN&lt;/TD&gt;&lt;TD&gt;col_date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;20-Jun-20&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;21-Jul-20&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Table b&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;status&lt;/TD&gt;&lt;TD&gt;status_date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;r&lt;/TD&gt;&lt;TD&gt;20-Aug-20&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;to get &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Table a&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;BAN&lt;/TD&gt;&lt;TD&gt;col_date&lt;/TD&gt;&lt;TD&gt;status&lt;/TD&gt;&lt;TD&gt;status_date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;20-Jun-20&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;21-Jul-20&lt;/TD&gt;&lt;TD&gt;r&lt;/TD&gt;&lt;TD&gt;20-Aug-20&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would appreciate any and all guidance. thank you&lt;/P&gt;</description>
      <pubDate>Tue, 15 Sep 2020 02:29:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/683840#M207158</guid>
      <dc:creator>TheNovice</dc:creator>
      <dc:date>2020-09-15T02:29:51Z</dc:date>
    </item>
    <item>
      <title>Re: Left join a date to row with max date where duplicate records exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/683849#M207165</link>
      <description>&lt;P&gt;There is no BAN in table b?&lt;/P&gt;</description>
      <pubDate>Tue, 15 Sep 2020 04:08:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/683849#M207165</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-09-15T04:08:45Z</dc:date>
    </item>
    <item>
      <title>Re: Left join a date to row with max date where duplicate records exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/683850#M207166</link>
      <description>Apologies, yes, the join key would be ban</description>
      <pubDate>Tue, 15 Sep 2020 04:10:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/683850#M207166</guid>
      <dc:creator>TheNovice</dc:creator>
      <dc:date>2020-09-15T04:10:38Z</dc:date>
    </item>
    <item>
      <title>Re: Left join a date to row with max date where duplicate records exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/683855#M207168</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/248692"&gt;@TheNovice&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Apologies, yes, the join key would be ban&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then, again: why is the variable BAN not in the second table?&lt;/P&gt;</description>
      <pubDate>Tue, 15 Sep 2020 04:49:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/683855#M207168</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-09-15T04:49:25Z</dc:date>
    </item>
    <item>
      <title>Re: Left join a date to row with max date where duplicate records exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/683871#M207174</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/248692"&gt;@TheNovice&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Apologies, yes, the join key would be ban&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;According to above I have added BAN to table b.&lt;/P&gt;
&lt;P&gt;Next code is tested:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table_a;
 input ban col_date :date9.;
 format col_date date9.;
cards;
123 20-jun-20
123 21-jul-20
; run;

data table_b;
 input ban status $ status_date :date9.;
 format status_date date9.;
cards;
123 r 20-aug-20
; run;

proc sql;
 create table want(drop=dif_date min_date) as
 select a.ban, a.col_date,
        b.status_date - a.col_date as dif_date,
        min(calculated dif_date) as min_date,
        case when calculated dif_date = calculated min_date
             then b.status 
             else  ' ' end as status ,
        case when calculated dif_date = calculated min_date
             then b.status_date 
             else  .   end as status_date format=date9.
 from table_a as a 
 left join table_b as b 
 on a.ban = b.ban 
 group by a.ban 
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Sep 2020 06:41:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/683871#M207174</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-09-15T06:41:17Z</dc:date>
    </item>
    <item>
      <title>Re: Left join a date to row with max date where duplicate records exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/683934#M207200</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table_a;
 input ban col_date :date9.;
 format col_date date9.;
cards;
123 20-jun-20
123 21-jul-20
; run;

data table_b;
 input ban status $ status_date :date9.;
 format status_date date9.;
cards;
123 r 20-aug-20
; run;

proc sql;
 create table want as
 select a.ban, a.col_date,

 case when abs(a.col_date-b.status_date)=
 min(abs(a.col_date-b.status_date)) then b.status
 else ' ' end as status
 ,
 case when abs(a.col_date-b.status_date)=
 min(abs(a.col_date-b.status_date)) then b.status_date
 else . end as status_date format=date9.

 from table_a as a 
 left join table_b as b 
 on a.ban = b.ban ;
 quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Sep 2020 11:49:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/683934#M207200</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-09-15T11:49:08Z</dc:date>
    </item>
    <item>
      <title>Re: Left join a date to row with max date where duplicate records exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/684030#M207238</link>
      <description>&lt;P&gt;I think &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt; 's answer doesn't quite answer the question. This might do better:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
 input ban col_date :date9.;
 format col_date date9.;
cards;
123 20-jun-20
123 21-jul-20
567 20-jun-20
567 21-jul-20
;

data b;
 input ban status $ status_date :date9.;
 format status_date date9.;
cards;
123 r 20-aug-20
456 r 20-aug-20
;

proc sql;
 
 select a.ban, col_date,

 case when col_date = max(col_date) then status
 else ' ' end as status
 ,
 case when col_date = max(col_date) then status_date
 else . end as status_date format=date9.

 from a 
 left join b 
 on a.ban = b.ban and status_date &amp;gt; col_date
 group by a.ban;
 quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 200px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49384i1000BF1B664CF97D/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Sep 2020 18:49:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/684030#M207238</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-09-15T18:49:46Z</dc:date>
    </item>
    <item>
      <title>Re: Left join a date to row with max date where duplicate records exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/684076#M207266</link>
      <description>Thank you all so much. all the solutions work &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; I will tag the first one with the accept then. thanks again</description>
      <pubDate>Tue, 15 Sep 2020 23:44:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/684076#M207266</guid>
      <dc:creator>TheNovice</dc:creator>
      <dc:date>2020-09-15T23:44:42Z</dc:date>
    </item>
    <item>
      <title>Re: Left join a date to row with max date where duplicate records exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/684167#M207303</link>
      <description>PG,&lt;BR /&gt;Yes. I misunderstood something in original post .</description>
      <pubDate>Wed, 16 Sep 2020 11:13:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-join-a-date-to-row-with-max-date-where-duplicate-records/m-p/684167#M207303</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-09-16T11:13:13Z</dc:date>
    </item>
  </channel>
</rss>

