<?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 to last record in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Left-join-to-last-record/m-p/354894#M83086</link>
    <description>&lt;P&gt;thanks Astounding.. ? though, there will be instances where there will be no record in table want_preliminary that match id from my original table. If I don't do a left join, will I not miss the records from my original table that got zero match during the join (I would need to keep all IDs/records from my original table)? thank you&lt;/P&gt;</description>
    <pubDate>Mon, 01 May 2017 11:05:57 GMT</pubDate>
    <dc:creator>brulard</dc:creator>
    <dc:date>2017-05-01T11:05:57Z</dc:date>
    <item>
      <title>Left join to last record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-join-to-last-record/m-p/354237#M82840</link>
      <description>&lt;P&gt;hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to join my table to the last date record of another table (an ID will have several records, where only the latest one is of interest).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can do this this way, but it has the inconvenience of needing me to first query every single last record from the other table, prior to my performing the join. Pls let me know if there is a better way of doing this. Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc sql;create table want_preliminary as&lt;/P&gt;&lt;P&gt;Select *&lt;/P&gt;&lt;P&gt;From table_warehouse&lt;/P&gt;&lt;P&gt;Group by&amp;nbsp; id&lt;/P&gt;&lt;P&gt;Having bill_yr_mth=max(bill_yr_mth);quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc sql;create table&amp;nbsp; want_final as&lt;/P&gt;&lt;P&gt;Select b.amtount,b.bill_yr_mth,a.*&lt;/P&gt;&lt;P&gt;From have as a left join want_preliminary as b on&lt;/P&gt;&lt;P&gt;a.id=b.id ;quit;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 18:55:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-join-to-last-record/m-p/354237#M82840</guid>
      <dc:creator>brulard</dc:creator>
      <dc:date>2017-04-27T18:55:38Z</dc:date>
    </item>
    <item>
      <title>Re: Left join to last record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-join-to-last-record/m-p/354251#M82844</link>
      <description>&lt;P&gt;I'm sure there are some SQL wizards that could make this happen in a single SELECT statement.&amp;nbsp; But the main improvement you can make is on the first SELECT.&amp;nbsp; Instead of SELECT *, bring in only the fields you need:&amp;nbsp; SELECT id, bill_yr_mth, amtount&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once that is in place, your second SELECT statement can be simplified:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;select a.*, b.* from have a, want_preliminary b where a.id=b.id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There's no need for a left join when you are checking for matching IDs.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 19:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-join-to-last-record/m-p/354251#M82844</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-04-27T19:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: Left join to last record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-join-to-last-record/m-p/354255#M82846</link>
      <description>&lt;P&gt;Right. &amp;nbsp;To add on to what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;said, you can do it in a single query, but it will not necessarily be more efficient. &amp;nbsp;Just less real estate on the code file (code not tested.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc sql;&lt;BR /&gt;create table want_final as
Select b.amtount,b.bill_yr_mth,a.*
From have as a 
inner join (
Select id, amtount, bill_yr_mth
From table_warehouse
Group by  id, amtount, bill_yr_mth
Having bill_yr_mth=max(bill_yr_mth) b
on b.id = a.id;
quit;
  &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 19:15:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-join-to-last-record/m-p/354255#M82846</guid>
      <dc:creator>thomp7050</dc:creator>
      <dc:date>2017-04-27T19:15:08Z</dc:date>
    </item>
    <item>
      <title>Re: Left join to last record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-join-to-last-record/m-p/354298#M82862</link>
      <description>&lt;P&gt;Same, syntax fixed (I think)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc sql;
create table want_final as
Select 
	b.amtount,
	b.bill_yr_mth,
	a.*
From 
	have as a inner join 
	(	Select id, amtount, bill_yr_mth
		From table_warehouse
		Group by  id
		Having bill_yr_mth=max(bill_yr_mth) ) as b
	on b.id = a.id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Apr 2017 22:26:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-join-to-last-record/m-p/354298#M82862</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-04-27T22:26:56Z</dc:date>
    </item>
    <item>
      <title>Re: Left join to last record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-join-to-last-record/m-p/354402#M82912</link>
      <description>thanks Astounding.. ? though, there will be instances where there will be no record in table want_preliminary that match id from my original table. If I don't do a left join, will I not miss the records from my original table that got zero match during the join (I would need to keep all IDs/records from my original table)? thank you</description>
      <pubDate>Fri, 28 Apr 2017 10:35:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-join-to-last-record/m-p/354402#M82912</guid>
      <dc:creator>brulard</dc:creator>
      <dc:date>2017-04-28T10:35:40Z</dc:date>
    </item>
    <item>
      <title>Re: Left join to last record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-join-to-last-record/m-p/354894#M83086</link>
      <description>&lt;P&gt;thanks Astounding.. ? though, there will be instances where there will be no record in table want_preliminary that match id from my original table. If I don't do a left join, will I not miss the records from my original table that got zero match during the join (I would need to keep all IDs/records from my original table)? thank you&lt;/P&gt;</description>
      <pubDate>Mon, 01 May 2017 11:05:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-join-to-last-record/m-p/354894#M83086</guid>
      <dc:creator>brulard</dc:creator>
      <dc:date>2017-05-01T11:05:57Z</dc:date>
    </item>
  </channel>
</rss>

