<?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: proc sql join one to last of group in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-join-one-to-last-of-group/m-p/35414#M8735</link>
    <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
Could you please try to adapt the following code to your needs and let me know if this is what you were expecting as SQL query ?&lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
Florent&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create table d as&lt;BR /&gt;
	select ds1.ID, num1, num2, name&lt;BR /&gt;
	from a ds1,&lt;BR /&gt;
                       b ds2&lt;BR /&gt;
	where ds1.ID = ds2.ID&lt;BR /&gt;
	  and ds2.num2 = (select max(num2)&lt;BR /&gt;
		            from b&lt;BR /&gt;
		            where ds1.ID = ID);&lt;BR /&gt;
quit;</description>
    <pubDate>Fri, 22 May 2009 09:23:22 GMT</pubDate>
    <dc:creator>Florent</dc:creator>
    <dc:date>2009-05-22T09:23:22Z</dc:date>
    <item>
      <title>proc sql join one to last of group</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-join-one-to-last-of-group/m-p/35413#M8734</link>
      <description>Hi! &lt;BR /&gt;
I have problem doing the join between two tables in EG or sql code. First data set has observations with unique id and second data set has n obs with that id and also a var (for example date) and I need to join obs from first data set to the obs from second data set with the latest date. Also many other vars in both data sets. Data step merge is easy but is it possible to do it in one sql join (preferrably using EG query builder)? See data step solution below.&lt;BR /&gt;
&lt;BR /&gt;
data a;&lt;BR /&gt;
   input id num1;&lt;BR /&gt;
   datalines;&lt;BR /&gt;
1 4&lt;BR /&gt;
2 5&lt;BR /&gt;
;&lt;BR /&gt;
data b;&lt;BR /&gt;
   input id num2 name $;&lt;BR /&gt;
   datalines;&lt;BR /&gt;
1 2 Br&lt;BR /&gt;
1 3 Qc&lt;BR /&gt;
1 7 Fe&lt;BR /&gt;
2 2 Dr&lt;BR /&gt;
2 4 Mi&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=a;&lt;BR /&gt;
by id;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=b;&lt;BR /&gt;
by id num2;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data c;&lt;BR /&gt;
   merge a b;&lt;BR /&gt;
   by id;&lt;BR /&gt;
   if last.id;&lt;BR /&gt;
run;&lt;BR /&gt;
proc print;run;</description>
      <pubDate>Fri, 22 May 2009 06:39:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-join-one-to-last-of-group/m-p/35413#M8734</guid>
      <dc:creator>foxrt</dc:creator>
      <dc:date>2009-05-22T06:39:34Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql join one to last of group</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-join-one-to-last-of-group/m-p/35414#M8735</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
Could you please try to adapt the following code to your needs and let me know if this is what you were expecting as SQL query ?&lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
Florent&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create table d as&lt;BR /&gt;
	select ds1.ID, num1, num2, name&lt;BR /&gt;
	from a ds1,&lt;BR /&gt;
                       b ds2&lt;BR /&gt;
	where ds1.ID = ds2.ID&lt;BR /&gt;
	  and ds2.num2 = (select max(num2)&lt;BR /&gt;
		            from b&lt;BR /&gt;
		            where ds1.ID = ID);&lt;BR /&gt;
quit;</description>
      <pubDate>Fri, 22 May 2009 09:23:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-join-one-to-last-of-group/m-p/35414#M8735</guid>
      <dc:creator>Florent</dc:creator>
      <dc:date>2009-05-22T09:23:22Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql join one to last of group</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-join-one-to-last-of-group/m-p/35415#M8736</link>
      <description>Thanks for you quick reply! It does exactly what I need. It's easy when you know it...</description>
      <pubDate>Fri, 22 May 2009 09:42:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-join-one-to-last-of-group/m-p/35415#M8736</guid>
      <dc:creator>foxrt</dc:creator>
      <dc:date>2009-05-22T09:42:52Z</dc:date>
    </item>
  </channel>
</rss>

