<?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: Maximum value of a column (variable) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28759#M5328</link>
    <description>Excellent! I got exactly what I wanted, and best of all the code is just a few lines.&lt;BR /&gt;
&lt;BR /&gt;
Thank-you very much everyone for your help</description>
    <pubDate>Fri, 11 Dec 2009 12:01:57 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2009-12-11T12:01:57Z</dc:date>
    <item>
      <title>Maximum value of a column (variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28751#M5320</link>
      <description>Hello every,&lt;BR /&gt;
&lt;BR /&gt;
I'm new to SAS and I need to find the maximum value of a variable, put that value in another dataset, and repeat for another category. Example:&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;&lt;U&gt;DATASET 1&lt;/U&gt;&lt;I&gt;&lt;BR /&gt;
V1 V2 V3&lt;BR /&gt;
01 AA 03&lt;BR /&gt;
01 BB 24&lt;BR /&gt;
01 CC 98&lt;BR /&gt;
02 DD 54&lt;BR /&gt;
02 EE 12&lt;BR /&gt;
02 FF 69&lt;BR /&gt;
03 GG 55&lt;BR /&gt;
03 HH 78&lt;BR /&gt;
03 KK 21&lt;BR /&gt;
&lt;/I&gt;&lt;BR /&gt;
&lt;BR /&gt;
&lt;U&gt;DATASET 2&lt;/U&gt;&lt;I&gt;&lt;BR /&gt;
V1 V2 V3&lt;BR /&gt;
01 CC 98&lt;BR /&gt;
02 FF 69&lt;BR /&gt;
03 HH 78&lt;BR /&gt;
&lt;/I&gt;&lt;/B&gt;&lt;BR /&gt;
&lt;BR /&gt;
I have absolutely no idea how to do this so any help at all would be very much appreciated.&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;&lt;U&gt;My code until now:&lt;/U&gt; (doubt it could be of any help but well...)&lt;/B&gt;&lt;BR /&gt;
&lt;I&gt;proc sort data=work.dataset1 out=work.temp; by V1 descending V3; run;&lt;BR /&gt;
DATA work.dataset2;&lt;BR /&gt;
	SET work.temp;&lt;BR /&gt;
	BY V1;&lt;BR /&gt;
/* trying to take the 1st row and add it to dataset2 */&lt;BR /&gt;
	IF (_N_=1) then do; &lt;BR /&gt;
		V1=V1; V2=V2; V3=V3;&lt;BR /&gt;
	END;&lt;BR /&gt;
RUN;&lt;/I&gt;&lt;BR /&gt;
&lt;BR /&gt;
Thank-you in advance.

Message was edited by: SanitySlayer42</description>
      <pubDate>Thu, 10 Dec 2009 18:49:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28751#M5320</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-12-10T18:49:23Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum value of a column (variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28752#M5321</link>
      <description>something like&lt;BR /&gt;
proc sql ;&lt;BR /&gt;
create table dataset2  as&lt;BR /&gt;
 select a.v1, a.v2, a.v3&lt;BR /&gt;
    from dataset1 as a&lt;BR /&gt;
    join ( select v1 from dataset1 &lt;BR /&gt;
             group by v1 having v2= max(v2) ) as b&lt;BR /&gt;
    on a.v1 = b.v2 &lt;BR /&gt;
  ;</description>
      <pubDate>Thu, 10 Dec 2009 19:10:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28752#M5321</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2009-12-10T19:10:13Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum value of a column (variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28753#M5322</link>
      <description>You want something like this.&lt;BR /&gt;
&lt;BR /&gt;
data try1;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input cat01 $ v2 $ v3;&lt;BR /&gt;
datalines;&lt;BR /&gt;
01 AA 03&lt;BR /&gt;
01 BB 24&lt;BR /&gt;
01 CC 98&lt;BR /&gt;
02 DD 54&lt;BR /&gt;
02 EE 12&lt;BR /&gt;
02 FF 69&lt;BR /&gt;
03 GG 55&lt;BR /&gt;
03 HH 78&lt;BR /&gt;
03 KK 21&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
/* method 1 */&lt;BR /&gt;
proc means nway max printidvars;&lt;BR /&gt;
class cat01;&lt;BR /&gt;
id v2;  /* saves the value if v2 corresponding to the max */&lt;BR /&gt;
output out=data2;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc print data=data2;&lt;BR /&gt;
where _stat_='MAX';&lt;BR /&gt;
var cat01 v2 v3;&lt;BR /&gt;
run;</description>
      <pubDate>Thu, 10 Dec 2009 22:18:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28753#M5322</guid>
      <dc:creator>mftuchman</dc:creator>
      <dc:date>2009-12-10T22:18:44Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum value of a column (variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28754#M5323</link>
      <description>As implied,here is method 2.  This uses the output statement more precisely to direct the output.  maxid(v3)=v2 means that you want an output for each value of cat01 to be the value of v2 that maximizes v3.   &lt;BR /&gt;
&lt;BR /&gt;
You still want to output the actual maximum, so also include max(v3)= to save the maximum value.&lt;BR /&gt;
[pre]&lt;BR /&gt;
/* method 2 */&lt;BR /&gt;
proc means data=try1 nway noprint;&lt;BR /&gt;
  class cat01;&lt;BR /&gt;
   id v2;&lt;BR /&gt;
   output out=data2 maxid(v3)=v2 max(v3)=;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
proc print data=data2;&lt;BR /&gt;
  var cat01 v2 v3;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: mftuchman

Message was edited by: mftuchman&lt;BR /&gt;
Added spacing.</description>
      <pubDate>Thu, 10 Dec 2009 22:24:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28754#M5323</guid>
      <dc:creator>mftuchman</dc:creator>
      <dc:date>2009-12-10T22:24:03Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum value of a column (variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28755#M5324</link>
      <description>sorry folks!&lt;BR /&gt;
my little piece of sql was untested and inevitably wrong!&lt;BR /&gt;
The objective I targetted was collecting the row of original data where the column V2 holds the maximum value within the group of V1. For that objective, V3 may not be the maximum value within the V1 group.&lt;BR /&gt;
A revised (and tested) version follows:&lt;BR /&gt;
proc sql ;&lt;BR /&gt;
create table dataset2 as select a.* from dataset1 as a&lt;BR /&gt;
join ( select v1, v2 from dataset1 group by v1 having v2= max(v2) )  as b&lt;BR /&gt;
on a.v1 = b.v1 and a.v2= b.v2&lt;BR /&gt;
;  &lt;BR /&gt;
quit ;&lt;BR /&gt;
 &lt;BR /&gt;
PeterC</description>
      <pubDate>Fri, 11 Dec 2009 09:42:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28755#M5324</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2009-12-11T09:42:35Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum value of a column (variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28756#M5325</link>
      <description>&lt;P&gt;&lt;EM&gt;Editor's Note: Thanks to all who contributed to this solution.&amp;nbsp; If the variable is numeric, there are also a number of procedures that can output the MAX statistic, including &lt;A href="http://support.sas.com/documentation/cdl/en/proc/68954/HTML/default/viewer.htm#p0f0fjpjeuco4gn1ri963f683mi4.htm" target="_self"&gt;PROC MEANS &lt;/A&gt;&amp;nbsp;and &lt;A href="http://support.sas.com/documentation/cdl/en/proc/68954/HTML/default/viewer.htm#p0bqogcics9o4xn17yvt2qjbgdpi.htm" target="_self"&gt;PROC REPORT&lt;/A&gt;.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's actually quite simple to do.&lt;BR /&gt; &lt;BR /&gt; After sorting your data the way is should (V1 descending V3), you just need to check the value of the FIRST automatic variable to identify the first observation of the V1 group.&lt;BR /&gt; &lt;BR /&gt; Here's you're code with the modification:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=work.dataset1 out=work.temp; 
 by V1 descending V3; 
run;

data work.dataset2;
 set work.temp;
 by V1;
 if first.V1; * first member of the group then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt; Cheers from Portugal&lt;BR /&gt; &lt;BR /&gt; Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Sep 2016 16:40:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28756#M5325</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2016-09-28T16:40:54Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum value of a column (variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28757#M5326</link>
      <description>Hello, thank-you all for your help.&lt;BR /&gt;
&lt;BR /&gt;
mftuchman's method doesn't appear to produce the correct result. It produces the dataset &lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;&lt;I&gt;V1 V2 V3&lt;BR /&gt;
01 CC 98&lt;BR /&gt;
02 FF 69&lt;BR /&gt;
03 KK 78&lt;/I&gt;&lt;/B&gt;&lt;I&gt;&lt;/I&gt;&lt;BR /&gt;
(note the difference when v1=03. It gives KK in V2 and not HH, hence is sorting it by V2 and not by V3)&lt;BR /&gt;
&lt;BR /&gt;
@Peter C: I thank-you very much for your help but unfortunately I am not meant to know about SQL just yet. I am meant to use something from the BASE package.&lt;BR /&gt;
&lt;BR /&gt;
@Daniel Santos	: This seems more like what I'm looking for but there is just a little problem. It outputs all the variables in the exact same order, how could I go about changing your code to remove/rearrange certain variables?&lt;BR /&gt;
&lt;BR /&gt;
Example:&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;&lt;U&gt;From this:&lt;/U&gt;&lt;BR /&gt;
V1 V2 V3&lt;BR /&gt;
01 AA 03&lt;BR /&gt;
01 BB 24&lt;BR /&gt;
01 CC 98&lt;BR /&gt;
02 DD 54&lt;BR /&gt;
02 EE 12&lt;BR /&gt;
02 FF 69&lt;BR /&gt;
03 GG 55&lt;BR /&gt;
03 HH 78&lt;BR /&gt;
03 KK 21&lt;BR /&gt;
&lt;BR /&gt;
&lt;U&gt;TO THIS:&lt;/U&gt;&lt;BR /&gt;
V2 V1&lt;BR /&gt;
98 01&lt;BR /&gt;
69 02&lt;BR /&gt;
78 03&lt;BR /&gt;
&lt;BR /&gt;
&lt;/B&gt;I haved tried by doing like so:&lt;BR /&gt;
&lt;I&gt;&lt;B&gt;proc sort data=TEMP; by V1 descending V3; run;&lt;BR /&gt;
&lt;BR /&gt;
DATA DATA2;&lt;BR /&gt;
	set TEMP;&lt;BR /&gt;
	by V1;&lt;BR /&gt;
	if first.Region then output V2 V3;&lt;BR /&gt;
RUN;&lt;/B&gt;&lt;/I&gt;&lt;BR /&gt;
&lt;BR /&gt;
But I get a &lt;I&gt;"Data set was not specified on the DATA statement."&lt;/I&gt; error. What must I do?</description>
      <pubDate>Fri, 11 Dec 2009 11:40:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28757#M5326</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-12-11T11:40:45Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum value of a column (variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28758#M5327</link>
      <description>OK.&lt;BR /&gt;
&lt;BR /&gt;
There is more than one way to rearrange the variable layout of a table.&lt;BR /&gt;
&lt;BR /&gt;
I usually do this with the LENGTH statement, which will redefine variables before reading the layout of your input table, thus reordering the layout.&lt;BR /&gt;
For this you will need to redefine the actual length and type of each variable (see link about the LENGTH statement).&lt;BR /&gt;
&lt;BR /&gt;
Dropping or keeping vars, can be done with the DROP/KEEP statement.&lt;BR /&gt;
&lt;BR /&gt;
something like this:&lt;BR /&gt;
&lt;BR /&gt;
[pre]data new_data;&lt;BR /&gt;
length FIRST_VAR 8 SECOND_VAR 8 THIRD_VAR $30; * redefine layout;&lt;BR /&gt;
set old_data;&lt;BR /&gt;
keep FIRST_VAR SECOND_VAR THIRD_VAR; * keep vars;&lt;BR /&gt;
run;[/pre]&lt;BR /&gt;
&lt;BR /&gt;
More about LENGTH statement:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000218807.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000218807.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
KEEP and DROP:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000202978.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000202978.htm&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000202902.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000202902.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Fri, 11 Dec 2009 11:53:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28758#M5327</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2009-12-11T11:53:35Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum value of a column (variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28759#M5328</link>
      <description>Excellent! I got exactly what I wanted, and best of all the code is just a few lines.&lt;BR /&gt;
&lt;BR /&gt;
Thank-you very much everyone for your help</description>
      <pubDate>Fri, 11 Dec 2009 12:01:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28759#M5328</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-12-11T12:01:57Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum value of a column (variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28760#M5329</link>
      <description>Yes, thank you for pointing that out.   However, I did not want to simply give up on maxid, since it was designed to do precisely what you asked.  You still consider it for future issues of the same type.  Here is the &lt;B&gt;corrected&lt;/B&gt; code.  I verified that it works.  &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data try1;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input v1 $ v2 $ v3;&lt;BR /&gt;
datalines;&lt;BR /&gt;
01 AA 03&lt;BR /&gt;
01 BB 24&lt;BR /&gt;
01 CC 98&lt;BR /&gt;
02 DD 54&lt;BR /&gt;
02 EE 12&lt;BR /&gt;
02 FF 69&lt;BR /&gt;
03 GG 55&lt;BR /&gt;
03 HH 78&lt;BR /&gt;
03 KK 21&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
/* method 1 */&lt;BR /&gt;
proc means data=try1 nway noprint;&lt;BR /&gt;
  class v1;&lt;BR /&gt;
  id v2;&lt;BR /&gt;
  var v3;&lt;BR /&gt;
  /* avoid duplicating the v2 variable&lt;BR /&gt;
     or the wrong result may be printed */&lt;BR /&gt;
  output out=fixedit maxid(v3(v2))=max_factor max=;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc print label;&lt;BR /&gt;
  label max_factor='v2';&lt;BR /&gt;
  id v1;&lt;BR /&gt;
  var max_factor v3;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
And I learned something too, so thanks.&lt;BR /&gt;
&lt;BR /&gt;
Generally, I like to use a single proc rather than glue things together with SQL if at all possible.

Message was edited by: mftuchman</description>
      <pubDate>Fri, 11 Dec 2009 14:40:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28760#M5329</guid>
      <dc:creator>mftuchman</dc:creator>
      <dc:date>2009-12-11T14:40:15Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum value of a column (variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28761#M5330</link>
      <description>Check out IDGROUP, OUTPUT statement option.  I think you may find it to be the superior replacement for ID and MAXID. &lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc means data=try1 nway noprint;&lt;BR /&gt;
   class v1;&lt;BR /&gt;
   output out=UsingIDGROUP idgroup(max(v3) out(v2 v3)=);&lt;BR /&gt;
   run;&lt;BR /&gt;
proc print;&lt;BR /&gt;
   run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Fri, 11 Dec 2009 15:05:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28761#M5330</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2009-12-11T15:05:34Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum value of a column (variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28762#M5331</link>
      <description>I appreciate the context.  I wish I had seen example like this in the documentation for IDGROUP.</description>
      <pubDate>Fri, 11 Dec 2009 16:29:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/28762#M5331</guid>
      <dc:creator>mftuchman</dc:creator>
      <dc:date>2009-12-11T16:29:59Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum value of a column (variable)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/285869#M58531</link>
      <description>&lt;P&gt;proc sort data=set1 out=set_sort;&lt;BR /&gt;by v1 descending v3;&lt;BR /&gt;run;&lt;BR /&gt;proc sort data=set_sort out=set_highest1 nodup;&lt;BR /&gt;by v1;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jul 2016 16:48:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-value-of-a-column-variable/m-p/285869#M58531</guid>
      <dc:creator>SAS_8</dc:creator>
      <dc:date>2016-07-20T16:48:04Z</dc:date>
    </item>
  </channel>
</rss>

