<?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 two tables based on date in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346721#M63578</link>
    <description>&lt;P&gt;&lt;STRONG&gt;Post test data in the form of a datastep.&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See other posts here, as noted I do not have time to enter your test data or guess if its the same strucutre or not.&lt;/P&gt;</description>
    <pubDate>Mon, 03 Apr 2017 15:29:23 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-04-03T15:29:23Z</dc:date>
    <item>
      <title>join two tables based on date</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346664#M63571</link>
      <description>&lt;P&gt;This is an example, I want to join tables A and B to obtain table c. As you can see I want to join tables on caldt if table C did not find TNA for the correspnding date in B then it took the previous observation&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table A&lt;/P&gt;
&lt;P&gt;caldt &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TNA&lt;/P&gt;
&lt;P&gt;21070327 &amp;nbsp; &amp;nbsp;20&lt;/P&gt;
&lt;P&gt;20170328 &amp;nbsp; &amp;nbsp;50&lt;/P&gt;
&lt;P&gt;20170329 &amp;nbsp; &amp;nbsp;120&lt;/P&gt;
&lt;P&gt;20170330 &amp;nbsp; &amp;nbsp; 50&lt;/P&gt;
&lt;P&gt;20170331 &amp;nbsp; &amp;nbsp; 87&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table B&lt;/P&gt;
&lt;P&gt;caldt &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mret&lt;/P&gt;
&lt;P&gt;20170325 &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;20170326 &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;21070327 &amp;nbsp; &amp;nbsp;0.45&lt;/P&gt;
&lt;P&gt;20170328 &amp;nbsp; &amp;nbsp;0.98&lt;/P&gt;
&lt;P&gt;20170329 &amp;nbsp; &amp;nbsp;0.67&lt;/P&gt;
&lt;P&gt;20170330 &amp;nbsp; &amp;nbsp; 0.50&lt;/P&gt;
&lt;P&gt;20170331 &amp;nbsp; &amp;nbsp; 0.87&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table C&lt;/P&gt;
&lt;P&gt;caldt &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mret &amp;nbsp; TNA&lt;/P&gt;
&lt;P&gt;20170325 &amp;nbsp; &amp;nbsp; . &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20&lt;/P&gt;
&lt;P&gt;20170326 &amp;nbsp; &amp;nbsp; . &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20&lt;/P&gt;
&lt;P&gt;21070327 &amp;nbsp; &amp;nbsp;0.45 &amp;nbsp; &amp;nbsp; 20&lt;/P&gt;
&lt;P&gt;20170328 &amp;nbsp; &amp;nbsp;0.98 &amp;nbsp; &amp;nbsp;50&lt;/P&gt;
&lt;P&gt;20170329 &amp;nbsp; &amp;nbsp;0.67 &amp;nbsp; &amp;nbsp;120&lt;/P&gt;
&lt;P&gt;20170330 &amp;nbsp; &amp;nbsp; 0.50 &amp;nbsp; &amp;nbsp;50&lt;/P&gt;
&lt;P&gt;20170331 &amp;nbsp; &amp;nbsp; 0.87 &amp;nbsp; &amp;nbsp;87&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2017 13:26:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346664#M63571</guid>
      <dc:creator>sasphd</dc:creator>
      <dc:date>2017-04-03T13:26:12Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables based on date</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346666#M63572</link>
      <description>&lt;P&gt;Post test data in the form of a datastep to get working code, best guess:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table C as 
  select  A.*,
            B.TNA
  from    A 
  right join B 
  on       A.CALDT=B.CALDT;
quit;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Apr 2017 13:31:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346666#M63572</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-03T13:31:20Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables based on date</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346676#M63573</link>
      <description>&lt;P&gt;Your program did not give me the right table C. It create a lot of "."&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2017 14:08:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346676#M63573</guid>
      <dc:creator>sasphd</dc:creator>
      <dc:date>2017-04-03T14:08:52Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables based on date</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346721#M63578</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Post test data in the form of a datastep.&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See other posts here, as noted I do not have time to enter your test data or guess if its the same strucutre or not.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2017 15:29:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346721#M63578</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-03T15:29:23Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables based on date</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346725#M63579</link>
      <description>&lt;P&gt;data A;&lt;BR /&gt;input caldt $ TNA $;&lt;BR /&gt;cards;&lt;BR /&gt;21070327 20&lt;BR /&gt;20170328 50&lt;BR /&gt;20170329 120&lt;BR /&gt;20170330 50&lt;BR /&gt;20170331 87&lt;BR /&gt;run;&lt;BR /&gt;data B;&lt;BR /&gt;input caldt $ mret $;&lt;BR /&gt;cards;&lt;BR /&gt;20170325 .&lt;BR /&gt;20170326 .&lt;BR /&gt;21070327 0.45&lt;BR /&gt;20170328 0.98&lt;BR /&gt;20170329 0.67&lt;BR /&gt;20170330 0.50&lt;BR /&gt;20170331 0.87&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2017 15:44:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346725#M63579</guid>
      <dc:creator>sasphd</dc:creator>
      <dc:date>2017-04-03T15:44:47Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables based on date</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346746#M63581</link>
      <description>&lt;P&gt;You should have got an error when that ran then as I had a and b the wrong way round (hence why its a good idea to always post test data in the form of a datastep so we can test):&lt;/P&gt;
&lt;PRE&gt;data A;
input caldt $ TNA $;
cards;
21070327 20
20170328 50
20170329 120
20170330 50
20170331 87
run;
data B;
input caldt $ mret $;
cards;
20170325 .
20170326 .
21070327 0.45
20170328 0.98
20170329 0.67
20170330 0.50
20170331 0.87
run;
proc sql;
  create table C as 
  select  B.*,
          A.TNA
  from    A 
  right join B 
  on       A.CALDT=B.CALDT;
quit;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Apr 2017 16:49:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346746#M63581</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-03T16:49:51Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables based on date</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346763#M63582</link>
      <description>&lt;P&gt;it did not give the right table C. I want that the program repeat the prevous observation of TNA if it does not exist in A.&lt;/P&gt;
&lt;P&gt;Please look carefully to the wanted table&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table C&lt;/P&gt;
&lt;P&gt;caldt &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mret &amp;nbsp; TNA&lt;/P&gt;
&lt;P&gt;20170325 &amp;nbsp; &amp;nbsp; . &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;STRONG&gt;20&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;20170326 &amp;nbsp; &amp;nbsp; . &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;STRONG&gt;&amp;nbsp; 20&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;21070327 &amp;nbsp; &amp;nbsp;0.45 &amp;nbsp; &amp;nbsp; 20&lt;/P&gt;
&lt;P&gt;20170328 &amp;nbsp; &amp;nbsp;0.98 &amp;nbsp; &amp;nbsp;50&lt;/P&gt;
&lt;P&gt;20170329 &amp;nbsp; &amp;nbsp;0.67 &amp;nbsp; &amp;nbsp;120&lt;/P&gt;
&lt;P&gt;20170330 &amp;nbsp; &amp;nbsp; 0.50 &amp;nbsp; &amp;nbsp;50&lt;/P&gt;
&lt;P&gt;20170331 &amp;nbsp; &amp;nbsp; 0.87 &amp;nbsp; &amp;nbsp;87&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2017 17:06:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346763#M63582</guid>
      <dc:creator>sasphd</dc:creator>
      <dc:date>2017-04-03T17:06:32Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables based on date</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346771#M63583</link>
      <description>&lt;P&gt;Sorry I make a mistake in table A.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I put aslo the table C as table to look to the result&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data A;&lt;BR /&gt;input caldt $ TNA $;&lt;BR /&gt;cards;&lt;BR /&gt;20170325 20&lt;BR /&gt;20170326 .&lt;BR /&gt;21070327 .&lt;BR /&gt;20170328 50&lt;BR /&gt;20170329 120&lt;BR /&gt;20170330 50&lt;BR /&gt;20170331 87&lt;BR /&gt;run;&lt;BR /&gt;data B;&lt;BR /&gt;input caldt $ mret $;&lt;BR /&gt;cards;&lt;BR /&gt;20170325 .&lt;BR /&gt;20170326 .&lt;BR /&gt;21070327 0.45&lt;BR /&gt;20170328 0.98&lt;BR /&gt;20170329 0.67&lt;BR /&gt;20170330 0.50&lt;BR /&gt;20170331 0.87&lt;BR /&gt;run;&lt;BR /&gt;data c ;&lt;BR /&gt;input caldt $ mret $ TNA $;&lt;BR /&gt;cards;&lt;BR /&gt;20170325 . 20&lt;BR /&gt;20170326 . 20&lt;BR /&gt;21070327 0.45 20&lt;BR /&gt;20170328 0.98 50&lt;BR /&gt;20170329 0.67 120&lt;BR /&gt;20170330 0.50 50&lt;BR /&gt;20170331 0.87 87&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2017 17:18:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346771#M63583</guid>
      <dc:creator>sasphd</dc:creator>
      <dc:date>2017-04-03T17:18:12Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables based on date</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346777#M63584</link>
      <description>&lt;P&gt;Edit, I noted in your test data you had 2107 year values, I assume these were typed incorrectly and fixed them.&lt;/P&gt;
&lt;P&gt;If the new data is true, then all you have to do is a minor update to table A before the merge:&lt;/P&gt;
&lt;PRE&gt;data A;
input caldt $ TNA $;
cards;
20170325 20
20170326 .
20170327 .
20170328 50
20170329 120
20170330 50
20170331 87
run;
data B;
input caldt $ mret $;
cards;
20170325 .
20170326 .
20170327 0.45
20170328 0.98
20170329 0.67
20170330 0.50
20170331 0.87
run;
data a;
  set a;
  retain lstv;
  if tna ne "" then lstv=tna;
  if tna="" then tna=lstv;
run;

proc sql;
  create table C as 
  select  B.*,
          A.TNA
  from    A 
  right join B 
  on       A.CALDT=B.CALDT;
quit;&lt;/PRE&gt;
&lt;P&gt;However, I assume here that the dates are really character, and that the first obs get copied down. &amp;nbsp;What your probably after, which is slightly different, is to take a baseline value and use that for missing elements. &amp;nbsp;In which case in your baseline data, create a flag (or logic) and pull that in and do a case when, for example if the first date was to be used for missings\;&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table C as 
  select  B.*,
          case when A.TNA is null then X.TNA else A.TNA end as TNA
  from    A 
  right join B 
  on       A.CALDT=B.CALDT
  left join (select TNA from A having input(CALDT,yymmdd10.)=min(input(CALDT,yymmdd10.))) X
  on      1=1;
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2017 17:33:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/join-two-tables-based-on-date/m-p/346777#M63584</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-03T17:33:40Z</dc:date>
    </item>
  </channel>
</rss>

