<?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: Select a column with the highest value in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Select-a-column-with-the-highest-value/m-p/712683#M80033</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input   id $  wht_rev  blk_rev  asn_rev iph_rev;
cards;
   b1    265        114          34        7

   b2    65          218         5          22

   b4    78          101        12          4
   ;

data want;
 set have;
 length want $32;
 array t(*) wht_rev--iph_rev;
 max=max(of t(*)); /*keep the max value in dataset*/
 want=scan(vname(t(whichn(max,of t(*)))),1,'_');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Please feel free to add a KEEP or DROP statement to choose what variables you need to remain or drop &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 20 Jan 2021 13:55:45 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2021-01-20T13:55:45Z</dc:date>
    <item>
      <title>Select a column with the highest value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Select-a-column-with-the-highest-value/m-p/712677#M80032</link>
      <description>&lt;P&gt;I want to extract the name from a variable based on the highest value in the fields.&lt;/P&gt;&lt;P&gt;here is a sample of the data&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;id&amp;nbsp; &amp;nbsp;wht_rev&amp;nbsp; blk_rev&amp;nbsp; asn_rev iph_rev&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;b1&amp;nbsp; &amp;nbsp; 265&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 114&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 34&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 7&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;b2&amp;nbsp; &amp;nbsp; 65&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 218&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 22&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;b4&amp;nbsp; &amp;nbsp; 78&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so i want to create a variable called ethnic based on the highest value from each row of the data set. the result should look like&lt;/P&gt;&lt;P&gt;&amp;nbsp;id&amp;nbsp; &amp;nbsp; ethnic&lt;/P&gt;&lt;P&gt;b1&amp;nbsp; &amp;nbsp; &amp;nbsp;wht&lt;/P&gt;&lt;P&gt;b2&amp;nbsp; &amp;nbsp; &amp;nbsp;blk&lt;/P&gt;&lt;P&gt;b4&amp;nbsp; &amp;nbsp; &amp;nbsp;blk&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am not sure where to begin at all&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2021 13:43:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Select-a-column-with-the-highest-value/m-p/712677#M80032</guid>
      <dc:creator>bibbnd</dc:creator>
      <dc:date>2021-01-20T13:43:36Z</dc:date>
    </item>
    <item>
      <title>Re: Select a column with the highest value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Select-a-column-with-the-highest-value/m-p/712683#M80033</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input   id $  wht_rev  blk_rev  asn_rev iph_rev;
cards;
   b1    265        114          34        7

   b2    65          218         5          22

   b4    78          101        12          4
   ;

data want;
 set have;
 length want $32;
 array t(*) wht_rev--iph_rev;
 max=max(of t(*)); /*keep the max value in dataset*/
 want=scan(vname(t(whichn(max,of t(*)))),1,'_');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Please feel free to add a KEEP or DROP statement to choose what variables you need to remain or drop &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2021 13:55:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Select-a-column-with-the-highest-value/m-p/712683#M80033</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-01-20T13:55:45Z</dc:date>
    </item>
    <item>
      <title>Re: Select a column with the highest value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Select-a-column-with-the-highest-value/m-p/712686#M80034</link>
      <description>&lt;P&gt;Try this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ wht_rev blk_rev asn_rev iph_rev;
datalines;
b1 265 114 34 7
b2 65 218 5 22
b4 78 101 12 4
;

data want(keep = id ethnic);
   set have;
   array a wht_rev -- iph_rev;
   idx = whichn(max(of a[*]), of a[*]);
   ethnic = scan(vname(a[idx]), 1, '_');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;id  ethnic 
b1  wht 
b2  blk 
b4  blk &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2021 13:58:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Select-a-column-with-the-highest-value/m-p/712686#M80034</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-20T13:58:14Z</dc:date>
    </item>
    <item>
      <title>Re: Select a column with the highest value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Select-a-column-with-the-highest-value/m-p/712691#M80035</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/149821"&gt;@bibbnd&lt;/a&gt;&amp;nbsp; Please be aware WHICHN is not really the greatest function if you have a very very wide dataset. That doesn't quite do the binary search as we would like. At least in my little experience with what I have tested so far, it's rather slow when we have to deal with very wide datasets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sometimes, cycling through the list is far cleaner, easier and surprisingly faster in my experience. So-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input   id $  wht_rev  blk_rev  asn_rev iph_rev;
cards;
   b1    265        114          34        7

   b2    65          218         5          22

   b4    78          101        12          4
   ;

data want;
 set have;
 array t(*) wht_rev--iph_rev;
 max=max(of t(*)); 
 length want $32;
 do _n_=1 to dim(t);
  if t(_n_)=max then do;
    want=scan(vname(t(_n_)),1,'_');
	leave;
  end;
 end;
run;
 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Agree, it's boring but it's convenient.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2021 14:10:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Select-a-column-with-the-highest-value/m-p/712691#M80035</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-01-20T14:10:56Z</dc:date>
    </item>
    <item>
      <title>Re: Select a column with the highest value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Select-a-column-with-the-highest-value/m-p/712722#M80036</link>
      <description>&lt;P&gt;If you don't like data steps, you could use proc transpose + proc sort (twice):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=transposed name=ethnic;
	by id;
	var _numeric_;
run;

proc sort data=transposed out=sorted;
	by id descending col1;
run;

proc sort data=sorted out=want(drop=col1) nodupkey;
	by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: Processing data thrice is, of course, less efficient than then data-step-solution already provided &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2021 15:02:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Select-a-column-with-the-highest-value/m-p/712722#M80036</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-01-20T15:02:02Z</dc:date>
    </item>
    <item>
      <title>Re: Select a column with the highest value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Select-a-column-with-the-highest-value/m-p/712728#M80037</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp; My SQL colleagues/folks here sitting besides me would rather have your solution as opposed to the rest. Not kidding, honestly here it seems the focus is ONLY credit risk loan performance and appetite and not SAS efficiency for some reason.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rule: What is simple that produces results is business for us. So Kudos!!&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2021 15:08:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Select-a-column-with-the-highest-value/m-p/712728#M80037</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-01-20T15:08:22Z</dc:date>
    </item>
  </channel>
</rss>

