<?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: Max by group + keep variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Max-by-group-keep-variables/m-p/401259#M97336</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;GROUP BY only indicates which observations are taken into account when calculating the max&lt;/P&gt;
&lt;P&gt;but you still keep all observations. You have to add a where close on the condition&lt;/P&gt;
&lt;P&gt;valueX=max(valueX)&lt;/P&gt;</description>
    <pubDate>Thu, 05 Oct 2017 10:02:50 GMT</pubDate>
    <dc:creator>gamotte</dc:creator>
    <dc:date>2017-10-05T10:02:50Z</dc:date>
    <item>
      <title>Max by group + keep variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-by-group-keep-variables/m-p/401254#M97335</link>
      <description>&lt;P&gt;Dear community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have this dataset to start from:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;case&amp;nbsp; /&amp;nbsp;&amp;nbsp; name&amp;nbsp;&amp;nbsp; /&amp;nbsp;&amp;nbsp;&amp;nbsp; valueX&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;001&amp;nbsp; /&amp;nbsp; A&amp;nbsp;&amp;nbsp; /&amp;nbsp;&amp;nbsp; 100&lt;/P&gt;
&lt;P&gt;002&amp;nbsp; /&amp;nbsp; A&amp;nbsp;&amp;nbsp; /&amp;nbsp;&amp;nbsp;&amp;nbsp; 80&lt;/P&gt;
&lt;P&gt;002&amp;nbsp; /&amp;nbsp; B&amp;nbsp;&amp;nbsp; /&amp;nbsp;&amp;nbsp;&amp;nbsp; 20&lt;/P&gt;
&lt;P&gt;003&amp;nbsp; /&amp;nbsp; A&amp;nbsp;&amp;nbsp; /&amp;nbsp;&amp;nbsp;&amp;nbsp; 20&lt;/P&gt;
&lt;P&gt;003&amp;nbsp; /&amp;nbsp; B&amp;nbsp;&amp;nbsp; /&amp;nbsp;&amp;nbsp;&amp;nbsp; 50&lt;/P&gt;
&lt;P&gt;003&amp;nbsp; /&amp;nbsp; C&amp;nbsp;&amp;nbsp; / &amp;nbsp;&amp;nbsp; 30&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I need is for each case, the maximum for valueX, but the output need also to have the column "name".&lt;/P&gt;
&lt;P&gt;So, I want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;001 / A /&amp;nbsp; 100&lt;/P&gt;
&lt;P&gt;002 / B /&amp;nbsp; 80&lt;/P&gt;
&lt;P&gt;003 / B /&amp;nbsp; 50&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I tried:&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;create table want as&lt;/P&gt;
&lt;P&gt;select case, name, max(valueX) as max_value&lt;/P&gt;
&lt;P&gt;from have&lt;/P&gt;
&lt;P&gt;group by case;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but this results in:&lt;/P&gt;
&lt;P&gt;001&amp;nbsp; /&amp;nbsp; A&amp;nbsp; /&amp;nbsp; 100&lt;/P&gt;
&lt;P&gt;002&amp;nbsp; /&amp;nbsp; A&amp;nbsp; /&amp;nbsp; 80&lt;/P&gt;
&lt;P&gt;002&amp;nbsp; /&amp;nbsp; B&amp;nbsp; /&amp;nbsp; 80&lt;/P&gt;
&lt;P&gt;003&amp;nbsp; /&amp;nbsp; A&amp;nbsp;&amp;nbsp; /&amp;nbsp;&amp;nbsp;&amp;nbsp; 50&lt;/P&gt;
&lt;P&gt;003&amp;nbsp; /&amp;nbsp; B&amp;nbsp;&amp;nbsp; /&amp;nbsp;&amp;nbsp;&amp;nbsp; 50&lt;/P&gt;
&lt;P&gt;003&amp;nbsp; /&amp;nbsp; C&amp;nbsp;&amp;nbsp; / &amp;nbsp;&amp;nbsp; 50&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What am I doing wrong?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2017 09:40:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-by-group-keep-variables/m-p/401254#M97335</guid>
      <dc:creator>fre</dc:creator>
      <dc:date>2017-10-05T09:40:04Z</dc:date>
    </item>
    <item>
      <title>Re: Max by group + keep variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-by-group-keep-variables/m-p/401259#M97336</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;GROUP BY only indicates which observations are taken into account when calculating the max&lt;/P&gt;
&lt;P&gt;but you still keep all observations. You have to add a where close on the condition&lt;/P&gt;
&lt;P&gt;valueX=max(valueX)&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2017 10:02:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-by-group-keep-variables/m-p/401259#M97336</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2017-10-05T10:02:50Z</dc:date>
    </item>
    <item>
      <title>Re: Max by group + keep variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-by-group-keep-variables/m-p/401274#M97341</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30622"&gt;@gamotte&lt;/a&gt;&amp;nbsp;has answered the SQL part, you could also just do a sort and datastep (an be included in other code):&lt;/P&gt;
&lt;PRE&gt;proc sort data=have out=want;
  by case name descending valuex;
run;
data want;
  set have;
  by case name;
  if first.name;
run;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2017 10:25:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-by-group-keep-variables/m-p/401274#M97341</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-10-05T10:25:20Z</dc:date>
    </item>
    <item>
      <title>Re: Max by group + keep variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-by-group-keep-variables/m-p/401279#M97344</link>
      <description>&lt;P&gt;Try the having clause:&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; one;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;length&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; cas name $&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;32&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt; valueX &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;8&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; cas name valueX;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;datalines&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;001 A 100&lt;/P&gt;
&lt;P&gt;002 A 80&lt;/P&gt;
&lt;P&gt;002 B 20&lt;/P&gt;
&lt;P&gt;003 A 20&lt;/P&gt;
&lt;P&gt;003 B 50&lt;/P&gt;
&lt;P&gt;003 C 30&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;sql&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;create&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;table&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; two &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;select&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; cas, name, max(valueX) &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; max_valueX&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; one&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;group&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; cas&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;having&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; valueX = max_valueX&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;quit&lt;/FONT&gt;&lt;/STRONG&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;&lt;FONT face="Courier New" size="3"&gt;//Fredrik&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2017 11:03:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-by-group-keep-variables/m-p/401279#M97344</guid>
      <dc:creator>FredrikE</dc:creator>
      <dc:date>2017-10-05T11:03:20Z</dc:date>
    </item>
    <item>
      <title>Re: Max by group + keep variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-by-group-keep-variables/m-p/401304#M97357</link>
      <description>&lt;P&gt;Hi FredrikE, gamotte, RW9,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thank you all very much for your answers.&amp;nbsp; There are more possibilities, but I can only mark 1 as the solution, so I picked the one closest to my original code.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2017 12:40:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-by-group-keep-variables/m-p/401304#M97357</guid>
      <dc:creator>fre</dc:creator>
      <dc:date>2017-10-05T12:40:39Z</dc:date>
    </item>
  </channel>
</rss>

