<?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 tables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689206#M209489</link>
    <description>&lt;P&gt;What is the logic of the join?&lt;/P&gt;</description>
    <pubDate>Tue, 06 Oct 2020 13:38:34 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-10-06T13:38:34Z</dc:date>
    <item>
      <title>Join tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689185#M209483</link>
      <description>&lt;P&gt;Hi, I have below tables that I want to join. Please help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See attached "Q.xlsx". Table1, Table2 and Result table.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2020 13:22:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689185#M209483</guid>
      <dc:creator>helloSAS</dc:creator>
      <dc:date>2020-10-06T13:22:24Z</dc:date>
    </item>
    <item>
      <title>Re: Join tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689188#M209484</link>
      <description>&lt;P&gt;Join by what variable(s)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Most of us will not download Excel files, they are security risk. Better you should provide the data as &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;SAS data step code&lt;/A&gt;.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2020 13:27:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689188#M209484</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-06T13:27:53Z</dc:date>
    </item>
    <item>
      <title>Re: Join tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689205#M209488</link>
      <description>&lt;P&gt;sure.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;let me know if this works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; table1;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;infile&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;datalines&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;delimiter&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;','&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; id dts_tab1 &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#008080"&gt;datetime20.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;datalines&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;11111 03JAN2020:08:06:03&lt;/P&gt;
&lt;P&gt;11111 05JAN2020:16:06:03&lt;/P&gt;
&lt;P&gt;11111 07JAN2020:08:06:03&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; table2;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;infile&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;datalines&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;delimiter&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;','&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; id dts_tab2 &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#008080"&gt;datetime20.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;datalines&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;11111 01JAN2020:08:06:03&lt;/P&gt;
&lt;P&gt;11111 01JAN2020:08:06:03&lt;/P&gt;
&lt;P&gt;11111 04JAN2020:08:06:03&lt;/P&gt;
&lt;P&gt;11111 04JAN2020:08:06:03&lt;/P&gt;
&lt;P&gt;11111 04JAN2020:08:06:03&lt;/P&gt;
&lt;P&gt;11111 06JAN2020:08:06:03&lt;/P&gt;
&lt;P&gt;11111 08JAN2020:08:06:03&lt;/P&gt;
&lt;P&gt;11111 08JAN2020:08:06:03&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; result;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;infile&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;datalines&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;delimiter&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;','&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; id dts_tab2 dts_tab1 &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#008080"&gt;datetime20.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;datalines&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;11111 01JAN2020:08:06:03 .&lt;/P&gt;
&lt;P&gt;11111 01JAN2020:08:06:03 .&lt;/P&gt;
&lt;P&gt;11111 04JAN2020:08:06:03 03JAN2020:08:06:03&lt;/P&gt;
&lt;P&gt;11111 04JAN2020:08:06:03 03JAN2020:08:06:03&lt;/P&gt;
&lt;P&gt;11111 04JAN2020:08:06:03 03JAN2020:08:06:03&lt;/P&gt;
&lt;P&gt;11111 06JAN2020:08:06:03 05JAN2020:16:06:03&lt;/P&gt;
&lt;P&gt;11111 08JAN2020:08:06:03 07JAN2020:08:06:03&lt;/P&gt;
&lt;P&gt;11111 08JAN2020:08:06:03 07JAN2020:08:06:03&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2020 13:36:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689205#M209488</guid>
      <dc:creator>helloSAS</dc:creator>
      <dc:date>2020-10-06T13:36:13Z</dc:date>
    </item>
    <item>
      <title>Re: Join tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689206#M209489</link>
      <description>&lt;P&gt;What is the logic of the join?&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2020 13:38:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689206#M209489</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-06T13:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: Join tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689210#M209492</link>
      <description>&lt;LI-SPOILER&gt;&amp;nbsp;&lt;/LI-SPOILER&gt;
&lt;P&gt;I want to join both the tables on ID variable. In the result table I want dts_tab2 where dts_tab1 is &amp;gt; dts_tab2. but I don't want to a many to many join. dts_tab2 should be the latest occurrence.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2020 13:50:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689210#M209492</guid>
      <dc:creator>helloSAS</dc:creator>
      <dc:date>2020-10-06T13:50:31Z</dc:date>
    </item>
    <item>
      <title>Re: Join tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689213#M209495</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21058"&gt;@helloSAS&lt;/a&gt;&amp;nbsp;wrote:
&lt;P class="1601992719788"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to join both the tables on ID variable. In the result table I want dts_tab2 where dts_tab1 is &amp;gt; dts_tab2. but I don't want to a many to many join. dts_tab2 should be the latest occurrence.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In your result table, you do not have dts_tab1&amp;gt;dts_tab2.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2020 13:58:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689213#M209495</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-06T13:58:56Z</dc:date>
    </item>
    <item>
      <title>Re: Join tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689218#M209499</link>
      <description>&lt;P&gt;sorry it should be dts_tab2 &amp;gt; dts_tab1 without many to many join and always capture latest occurrence of dts_tab1. for ex: 6th row in result table I want dts_tab1 as 05JAN.. and not 03JAN...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2020 14:06:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689218#M209499</guid>
      <dc:creator>helloSAS</dc:creator>
      <dc:date>2020-10-06T14:06:47Z</dc:date>
    </item>
    <item>
      <title>Re: Join tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689224#M209501</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21058"&gt;@helloSAS&lt;/a&gt;&amp;nbsp; Is this what you're looking for?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
    infile datalines;
    input id dts_tab1 :datetime20.;
    format dts_tab1 datetime20.;
    datalines;
    11111 03JAN2020:08:06:03
    11111 05JAN2020:16:06:03
    11111 07JAN2020:08:06:03
    ;
run;

data table2;
    infile datalines;
    input id dts_tab2 :datetime20.;
    format dts_tab2 datetime20.;
    datalines;
    11111 01JAN2020:08:06:03
    11111 01JAN2020:08:06:03
    11111 04JAN2020:08:06:03
    11111 04JAN2020:08:06:03
    11111 04JAN2020:08:06:03
    11111 06JAN2020:08:06:03
    11111 08JAN2020:08:06:03
    11111 08JAN2020:08:06:03
    ;
run;

data table2_nobs;
    set table2;
    n = _n_;
run;

proc sql;
    create table work.joined as
    select a.id
          ,a.n
          ,a.dts_tab2 format datetime20.
          ,b.dts_tab1 format datetime20.
    from table2_nobs a
    left join table1 b
        on a.id = b.id
        and a.dts_tab2 &amp;gt; b.dts_tab1
    order by n, dts_tab1;
quit;

data work.want (drop=n);
    set joined;
    by n dts_tab1;
    if last.n;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mklangley_0-1601994680592.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50221i843205A642B9566E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="mklangley_0-1601994680592.png" alt="mklangley_0-1601994680592.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2020 14:31:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689224#M209501</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2020-10-06T14:31:39Z</dc:date>
    </item>
    <item>
      <title>Re: Join tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689225#M209502</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21058"&gt;@helloSAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;for ex: 6th row in result table I want dts_tab1 as 05JAN.. and not 03JAN...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Show me the desired table as adjusted by this explanation.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2020 14:33:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-tables/m-p/689225#M209502</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-06T14:33:05Z</dc:date>
    </item>
  </channel>
</rss>

