<?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 (max) function in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-max-function/m-p/231928#M54635</link>
    <description>I did this: &lt;BR /&gt;PROC SQL;&lt;BR /&gt; SELECT A.*,B.IDCERTKEY FROM GROUPA A,&lt;BR /&gt; 				 ID2B   B 			&lt;BR /&gt;  WHERE A.IDKEY = B.IDKEY&lt;BR /&gt;    AND B.IDCERTKEY = (SELECT MAX(DISTINCT IDCERTKEY) &lt;BR /&gt; 			  			 FROM ID2B C&lt;BR /&gt; 			  			WHERE B.IDKEY = C.IDKEY)  and AND (p.SAS_STU_START_DATE LE e.SAS_WITHDRLDATE_DATE LE p.SAS_STU_END_DATE) ;&lt;BR /&gt;QUIT;&lt;BR /&gt;&lt;BR /&gt;I added the date qualifier at the end of the code and Its taking quite awhile- did I put the date parameter in the correct context of the syntax?</description>
    <pubDate>Tue, 27 Oct 2015 22:42:23 GMT</pubDate>
    <dc:creator>SannaSanna</dc:creator>
    <dc:date>2015-10-27T22:42:23Z</dc:date>
    <item>
      <title>Proc SQL (max) function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-max-function/m-p/231918#M54627</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hello!&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Would appreciate some help&amp;nbsp;with my&amp;nbsp;syntx.&amp;nbsp; I have two tables (A) and (B). I am trying to match all the records in table (A) to one of the many records in table (B)&amp;nbsp; Variable column &amp;nbsp;[B.IDCERTKEY] is a unique numerical primary key in table (B) with the most recent/newest record being the highest number.&amp;nbsp; There could be various [B.IDCERTKEY[ for one person identified as [(IDKEY)].&amp;nbsp; I want to tie the max(B.IDCERTKEY) to the matching person [IDKEY] to table A.&amp;nbsp; Can someone help me with the correct syntax so I can accomplish this?&amp;nbsp; My partial code is below.&amp;nbsp; &amp;nbsp;Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC SQL;&lt;BR /&gt;&amp;nbsp;CREATE TABLE NEW AS&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;SELECT A.*, B.IDKEY, B.IDCERTKEY, B.IDENCTYMSNGINDCTR, B.IDCATGCODE1KEY,&lt;BR /&gt;&amp;nbsp;&amp;nbsp;B.IDCATGCODE2KEY, B.IDCATGCODE3KEY,B.IDCATGCODE4KEY,&lt;BR /&gt;&amp;nbsp;&amp;nbsp;B.IDCATGCODE5KEY,&amp;nbsp; &lt;BR /&gt;FROM GROUPA AS A LEFT JOIN ID2B AS B&lt;BR /&gt;ON A.IDKEY=B.IDKEY AND (A.SAS_ID_START_DATE LE B.SAS_WITHDRLDATE_DATE LE A.SAS_ID_END_DATE) ;&lt;BR /&gt;QUIT;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Oct 2015 21:33:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-max-function/m-p/231918#M54627</guid>
      <dc:creator>SannaSanna</dc:creator>
      <dc:date>2015-10-27T21:33:43Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL (max) function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-max-function/m-p/231924#M54631</link>
      <description>&lt;P&gt;Hello - Hope this what you are looking for ....&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;data GROUPA;
     input IDKEY:4. VAR:$1.;
     cards;
     2014 A
     2014 B
     2015 C
     2016 D
;
RUN;
data ID2B;
     input IDKEY:4. IDCERTKEY:2.;
     cards;
     2014 01 
     2014 02 
     2015 03 
     2016 04 
;
RUN;
PROC SQL;
 SELECT A.*,B.IDCERTKEY FROM GROUPA A,
 				 ID2B   B 			
  WHERE A.IDKEY = B.IDKEY
    AND B.IDCERTKEY = (SELECT MAX(DISTINCT IDCERTKEY) 
 			  			 FROM ID2B C
 			  			WHERE B.IDKEY = C.IDKEY) ;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Produces the following: (the right most column has the highest values that you are looking for&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
IDKEY	VAR	IDCERTKEY
2014	A	2
2014	B	2
2015	C	3
2016	D	4&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Good Luck...!!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Oct 2015 22:18:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-max-function/m-p/231924#M54631</guid>
      <dc:creator>kannand</dc:creator>
      <dc:date>2015-10-27T22:18:23Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL (max) function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-max-function/m-p/231926#M54633</link>
      <description>You could try group by with having max (idcertkey).</description>
      <pubDate>Tue, 27 Oct 2015 22:19:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-max-function/m-p/231926#M54633</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2015-10-27T22:19:09Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL (max) function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-max-function/m-p/231928#M54635</link>
      <description>I did this: &lt;BR /&gt;PROC SQL;&lt;BR /&gt; SELECT A.*,B.IDCERTKEY FROM GROUPA A,&lt;BR /&gt; 				 ID2B   B 			&lt;BR /&gt;  WHERE A.IDKEY = B.IDKEY&lt;BR /&gt;    AND B.IDCERTKEY = (SELECT MAX(DISTINCT IDCERTKEY) &lt;BR /&gt; 			  			 FROM ID2B C&lt;BR /&gt; 			  			WHERE B.IDKEY = C.IDKEY)  and AND (p.SAS_STU_START_DATE LE e.SAS_WITHDRLDATE_DATE LE p.SAS_STU_END_DATE) ;&lt;BR /&gt;QUIT;&lt;BR /&gt;&lt;BR /&gt;I added the date qualifier at the end of the code and Its taking quite awhile- did I put the date parameter in the correct context of the syntax?</description>
      <pubDate>Tue, 27 Oct 2015 22:42:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-max-function/m-p/231928#M54635</guid>
      <dc:creator>SannaSanna</dc:creator>
      <dc:date>2015-10-27T22:42:23Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL (max) function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-max-function/m-p/231930#M54637</link>
      <description>&lt;P&gt;You seem to have two ANDs. Please check the syntax.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Oct 2015 22:48:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-max-function/m-p/231930#M54637</guid>
      <dc:creator>kannand</dc:creator>
      <dc:date>2015-10-27T22:48:15Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL (max) function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-max-function/m-p/231936#M54638</link>
      <description>Ohh thank you thank you!!!  It works!!!  Is there a way I can see the 'join' statement in the syntax?  I need a full left join so that all rows will return back for table (A) even if there is no match in table (B).</description>
      <pubDate>Wed, 28 Oct 2015 01:07:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-max-function/m-p/231936#M54638</guid>
      <dc:creator>SannaSanna</dc:creator>
      <dc:date>2015-10-28T01:07:34Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL (max) function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-max-function/m-p/231937#M54639</link>
      <description>&lt;P&gt;Here is how it looks with the LEFT JOIN...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data GROUPA;
     input IDKEY:4. VAR:$1.;
     cards;
     2014 A
     2014 B
     2015 C
     2016 D
     2013 E
;
RUN;
data ID2B;
     input IDKEY:4. IDCERTKEY:2.;
     cards;
     2014 01 
     2014 02 
     2015 03 
     2016 04 
;
RUN;
PROC SQL;
 SELECT A.*,B.IDCERTKEY FROM GROUPA A left join 
 				 ID2B   B 			on 
  		A.IDKEY = B.IDKEY
    AND B.IDCERTKEY = (SELECT MAX(DISTINCT IDCERTKEY) 
 			  			 FROM ID2B C
 			  			WHERE B.IDKEY = C.IDKEY) ;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And the output shows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
IDKEY	VAR	IDCERTKEY
2013	E	.
2014	A	2
2014	B	2
2015	C	3
2016	D	4&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps...!!!&lt;/P&gt;</description>
      <pubDate>Wed, 28 Oct 2015 01:31:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-max-function/m-p/231937#M54639</guid>
      <dc:creator>kannand</dc:creator>
      <dc:date>2015-10-28T01:31:10Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL (max) function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-max-function/m-p/231940#M54640</link>
      <description>ohh thank you so much!  You are just awesome!  I can now finally go home.  Thank you again!</description>
      <pubDate>Wed, 28 Oct 2015 01:44:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-max-function/m-p/231940#M54640</guid>
      <dc:creator>SannaSanna</dc:creator>
      <dc:date>2015-10-28T01:44:32Z</dc:date>
    </item>
  </channel>
</rss>

