<?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: How to - Get  Maximum Row's Value with its Name of Variable into New Column? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-Get-Maximum-Row-s-Value-with-its-Name-of-Variable-into/m-p/326737#M72821</link>
    <description>&lt;PRE&gt;data want;
   set have;
   array v Value1 Value2 Value3;
   maxvalue = max(of v(*));
   length maxvar $32.;
   maxvar = vname(v[whichn(maxvalue, of v(*))]);
run;
&lt;/PRE&gt;</description>
    <pubDate>Mon, 23 Jan 2017 15:17:55 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2017-01-23T15:17:55Z</dc:date>
    <item>
      <title>How to - Get  Maximum Row's Value with its Name of Variable into New Column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Get-Maximum-Row-s-Value-with-its-Name-of-Variable-into/m-p/326629#M72780</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I try to get maximum value of rows with its maximum value column. I mean that I also see the Variable which is belong the Maximum value. I have a sample data set as below, also I have desired output as below. Can somebody help me, please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
Length CustomerID 8 Value1 8 Value2 8 Value3 8;
Infile Datalines MISSOVER;
Input CustomerID Value1 Value2 Value3 ;
DATALINES;
1 5 10 15
2 3 8 9
3 4 9 2
4 1 12 9
5 12 8 7
6 15 7 4
7 2 8 19
8 18 32 5
9 8 9 7
10 7 12 8
;
Run;

PROC SQL;
Create Table Want As
Select CustomerID,Value1,Value2,Value3,Max(Value1,Value2,Value3) As MaxValue
From Have
Having Max(Value1,Value2,Value3);
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Desired output;&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/6841iD3882DD6692D8C61/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="MaxValue.png" title="MaxValue.png" /&gt;&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;</description>
      <pubDate>Mon, 23 Jan 2017 08:29:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Get-Maximum-Row-s-Value-with-its-Name-of-Variable-into/m-p/326629#M72780</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2017-01-23T08:29:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to - Get  Maximum Row's Value with its Name of Variable into New Column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Get-Maximum-Row-s-Value-with-its-Name-of-Variable-into/m-p/326634#M72782</link>
      <description>&lt;P&gt;Your condition&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Having Max(Value1,Value2,Value3)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is useless. The result of Max(Value1,Value2,Value3) is always &amp;gt; 0 and therefore a boolean true.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is best done (IMO) in a data step with array processing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array values {*} value1-value3;
maxval = 0;
maxind = 0;
do i = 1 to dim(values);
  if values{i} &amp;gt; maxval
  then do;
    maxval = values{i};
    maxind = i;
  end;
end;
maxvalname = vname(values{maxind});
maxval = values(maxind);
keep customerid maxval maxvalname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It's a quick shot and might be optimizable, but it delivers the intended result.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Jan 2017 08:44:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Get-Maximum-Row-s-Value-with-its-Name-of-Variable-into/m-p/326634#M72782</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-01-23T08:44:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to - Get  Maximum Row's Value with its Name of Variable into New Column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Get-Maximum-Row-s-Value-with-its-Name-of-Variable-into/m-p/326640#M72786</link>
      <description>&lt;P&gt;Hi.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's another way, if that's OK for you to do it in two steps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc transpose data=HAVE out=HAVE2 (rename=(_NAME_=MaxValueOf COL1=MaxValue));&lt;BR /&gt;by CUSTOMERID;&lt;BR /&gt;var VALUE1 VALUE2 VALUE3;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;create table WANT as&lt;BR /&gt;select a.CUSTOMERID, max(a.VALUE1, a.VALUE2, a.VALUE3) as MaxValue, b.MaxValueOf label=''&lt;BR /&gt;from HAVE as a, HAVE2 as b&lt;BR /&gt;where a.CUSTOMERID = b.CUSTOMERID and calculated MaxValue = b.MaxValue;&lt;BR /&gt;quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Fist step is to transpose data to create column MaxValueOf which holds the name of the value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second step will calculate max and match it against the named value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope it helps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Daniel Santos&amp;nbsp;@ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Jan 2017 09:13:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Get-Maximum-Row-s-Value-with-its-Name-of-Variable-into/m-p/326640#M72786</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2017-01-23T09:13:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to - Get  Maximum Row's Value with its Name of Variable into New Column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Get-Maximum-Row-s-Value-with-its-Name-of-Variable-into/m-p/326641#M72787</link>
      <description>&lt;P&gt;I would agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;that arrays are probably simplest, for instance if you have lots of "value"'s or don't know the number. &amp;nbsp;For this specific instance however a simple case when can do the trick:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table WANT as
  select  CUSTOMERID,
          max(VALUE1,VALUE2,VALUE3) as MAXVALUE,
          case  when VALUE1=CALCULATED MAXVALUE then "Value1"
                when VALUE2=CALCULATED MAXVALUE then "Value2"
                when VALUE3=CALCULATED MAXVALUE then "Value3"
                else "" end as MAXVALUEOF
  from    HAVE;
quit;&lt;/PRE&gt;
&lt;P&gt;Note that if multiple values have the max, then only the first will be the one given.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Jan 2017 09:25:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Get-Maximum-Row-s-Value-with-its-Name-of-Variable-into/m-p/326641#M72787</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-01-23T09:25:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to - Get  Maximum Row's Value with its Name of Variable into New Column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Get-Maximum-Row-s-Value-with-its-Name-of-Variable-into/m-p/326737#M72821</link>
      <description>&lt;PRE&gt;data want;
   set have;
   array v Value1 Value2 Value3;
   maxvalue = max(of v(*));
   length maxvar $32.;
   maxvar = vname(v[whichn(maxvalue, of v(*))]);
run;
&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Jan 2017 15:17:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Get-Maximum-Row-s-Value-with-its-Name-of-Variable-into/m-p/326737#M72821</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-01-23T15:17:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to - Get  Maximum Row's Value with its Name of Variable into New Column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Get-Maximum-Row-s-Value-with-its-Name-of-Variable-into/m-p/326741#M72823</link>
      <description>&lt;P&gt;I knew there would be something more elegant than just iterating through the array &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Jan 2017 15:23:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Get-Maximum-Row-s-Value-with-its-Name-of-Variable-into/m-p/326741#M72823</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-01-23T15:23:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to - Get  Maximum Row's Value with its Name of Variable into New Column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Get-Maximum-Row-s-Value-with-its-Name-of-Variable-into/m-p/326762#M72831</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I knew there would be something more elegant than just iterating through the array &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I have to thank &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;for showing me&amp;nbsp;the whichn function in a post. That, and whichc,&amp;nbsp;had snuck into to the SAS lexicon while I was working in an SPSS shop.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Jan 2017 16:01:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Get-Maximum-Row-s-Value-with-its-Name-of-Variable-into/m-p/326762#M72831</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-01-23T16:01:42Z</dc:date>
    </item>
  </channel>
</rss>

