<?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: Pulling Variable Header for Lowest Value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972789#M377566</link>
    <description>&lt;P&gt;This is great. But naturally I've over simplified my example. My data would be more representative of:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data work.have;&lt;BR /&gt;input Alabama Arkansas Indiana;&lt;BR /&gt;datalines;&lt;BR /&gt;100 200 250&lt;BR /&gt;200 100 275&lt;BR /&gt;300 900 100&lt;BR /&gt;250 25 120&lt;BR /&gt;50 8 65&lt;BR /&gt;400 700 200&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Mon, 18 Aug 2025 16:24:54 GMT</pubDate>
    <dc:creator>Lost_Gary</dc:creator>
    <dc:date>2025-08-18T16:24:54Z</dc:date>
    <item>
      <title>Pulling Variable Header for Lowest Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972782#M377564</link>
      <description>&lt;P&gt;Is there a way to pull the variable column header based on the lowest value within a dataset?&amp;nbsp; Such as:&lt;/P&gt;
&lt;TABLE width="205"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="47"&gt;Have1&lt;/TD&gt;
&lt;TD width="47"&gt;Have2&lt;/TD&gt;
&lt;TD width="47"&gt;Have3&lt;/TD&gt;
&lt;TD width="64"&gt;Want&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;100&lt;/TD&gt;
&lt;TD&gt;200&lt;/TD&gt;
&lt;TD&gt;250&lt;/TD&gt;
&lt;TD&gt;Have1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;200&lt;/TD&gt;
&lt;TD&gt;100&lt;/TD&gt;
&lt;TD&gt;275&lt;/TD&gt;
&lt;TD&gt;Have2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;300&lt;/TD&gt;
&lt;TD&gt;900&lt;/TD&gt;
&lt;TD&gt;100&lt;/TD&gt;
&lt;TD&gt;Have3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;250&lt;/TD&gt;
&lt;TD&gt;25&lt;/TD&gt;
&lt;TD&gt;120&lt;/TD&gt;
&lt;TD&gt;Have2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;50&lt;/TD&gt;
&lt;TD&gt;8&lt;/TD&gt;
&lt;TD&gt;65&lt;/TD&gt;
&lt;TD&gt;Have2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;400&lt;/TD&gt;
&lt;TD&gt;700&lt;/TD&gt;
&lt;TD&gt;200&lt;/TD&gt;
&lt;TD&gt;Have3&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;I have a very large number of variable to compare.&amp;nbsp; Thanks in advance.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Aug 2025 14:42:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972782#M377564</guid>
      <dc:creator>Lost_Gary</dc:creator>
      <dc:date>2025-08-18T14:42:01Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling Variable Header for Lowest Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972785#M377565</link>
      <description>&lt;P&gt;It sounds like you want to find the NAME of the variable with the smallest value.&lt;/P&gt;
&lt;P&gt;In your case you want to search the variables named HAVE1 - HAVE3 .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You probably will want to define an array and then use the MIN(), WHICHN() and VNAME() functions.&lt;/P&gt;
&lt;P&gt;First let's convert your LISTING into an actual DATASET.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Have1-Have3  Expect :$32.;
cards;
100  200  250  Have1
200  100  275  Have2
300  900  100  Have3
250   25  120  Have2
 50    8   65  Have2
400  700  200  Have3
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now we can run this data step to create the WANT variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array x have1-have3;
  Want=vname(x[whichn(min(of x[*]),of x[*])]);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt; Obs    Have1    Have2    Have3    Expect    Want

  1      100      200      250     Have1     Have1
  2      200      100      275     Have2     Have2
  3      300      900      100     Have3     Have3
  4      250       25      120     Have2     Have2
  5       50        8       65     Have2     Have2
  6      400      700      200     Have3     Have3&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Aug 2025 15:28:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972785#M377565</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-08-18T15:28:28Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling Variable Header for Lowest Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972789#M377566</link>
      <description>&lt;P&gt;This is great. But naturally I've over simplified my example. My data would be more representative of:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data work.have;&lt;BR /&gt;input Alabama Arkansas Indiana;&lt;BR /&gt;datalines;&lt;BR /&gt;100 200 250&lt;BR /&gt;200 100 275&lt;BR /&gt;300 900 100&lt;BR /&gt;250 25 120&lt;BR /&gt;50 8 65&lt;BR /&gt;400 700 200&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Aug 2025 16:24:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972789#M377566</guid>
      <dc:creator>Lost_Gary</dc:creator>
      <dc:date>2025-08-18T16:24:54Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling Variable Header for Lowest Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972791#M377567</link>
      <description>&lt;P&gt;Think about how you will handle ties, if ties are possible.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think Tom's lovely solution would return the first variable in the array found by WHICHN.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Aug 2025 16:25:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972791#M377567</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2025-08-18T16:25:10Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling Variable Header for Lowest Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972792#M377568</link>
      <description>&lt;P&gt;Different data structure should only require a change to the ARRAY statement, to list the variables in the array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or if you can put all variables in the array, you could try something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array x _numeric_ ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Aug 2025 16:27:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972792#M377568</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2025-08-18T16:27:29Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling Variable Header for Lowest Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972794#M377569</link>
      <description>&lt;P&gt;The idea of having variables names Alabama, Arkansas and Indiana seems opposite of the way SAS was designed to be most efficient. There's a reason why people say Long beats Wide, and normally it is because there is much less programming needed if your data is arrange properly, and probably the code will execute faster. In addition, people say data belongs in SAS variable values, not in variable names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So here's a long data set with the same data, and with the state names as data instead of variable names. Note that you do not have to type in the variable names Alabama, Arkansas and Indiana into your code, and if there are other states in your data, the&amp;nbsp;&lt;EM&gt;exact same code&lt;/EM&gt; works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
    input id value state $;
    cards;
   1     100      AL
   1     200      AR
   1     250      IN
   2     200      AL
   2     100      AR
   2     275      IN
   3     300      AL
   3     900      AR
   3     100      IN
   4     250      AL
   4      25      AR
   4     120      IN
   5      50      AL
   5       8      AR
   5      65      IN
   6     400      AL
   6     700      AR
   6     200      IN
;

proc summary data=have nway;
    class id;
    var value;
    output out=min min=minvalue minid(value(state))=min_state;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, in this very simple example, maybe the benefit isn't obvious, but in real world problems, long does beat wide, it works better in SAS because most SAS PROCs are designed to work on long data sets, and it will be less programming.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Aug 2025 17:02:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972794#M377569</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-08-18T17:02:05Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling Variable Header for Lowest Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972797#M377570</link>
      <description>&lt;P&gt;Just change the list of variables in the ARRAY statement.&lt;/P&gt;
&lt;P&gt;If you know they all appear together in the dataset then you could use a positional variable list (what the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p08ywlo51p6ezbn1lde9f0aphq6r.htm" target="_self"&gt;documentation&lt;/A&gt; calls a "name range list") as opposed the simple variable list that works with numeric suffixed names (what the documentation calls a "numbered range variable list").&amp;nbsp; Run PROC CONTENTS with the VARNUM option to see the variable names listed in order of where they appear in the dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if the variables are in order from Alaska to Wyoming then the array statement could simply be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array x Alaska -- Wyoming ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Aug 2025 17:10:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972797#M377570</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-08-18T17:10:56Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling Variable Header for Lowest Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972847#M377573</link>
      <description>&lt;P&gt;Another solution is using SAS/IML , if you have it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Have1-Have3  Expect :$32.;
cards;
100  200  250  Have1
200  100  275  Have2
300  900  100  Have3
250   25  120  Have2
 50    8   65  Have2
400  700  200  Have3
;

proc iml;
use have;
read all var _num_ into x[c=vname];
close;&lt;BR /&gt;
want=vname[x[,&amp;gt;:&amp;lt;]];&lt;BR /&gt;
create want from x want[c=(vname||'want')];
append from x want;
close;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Aug 2025 01:32:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972847#M377573</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-08-19T01:32:49Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling Variable Header for Lowest Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972963#M377614</link>
      <description>&lt;P&gt;Hi Folks!,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope this helps!.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This can be done using SAS arrays easily. Below code works fine to find the header of lowest value in&amp;nbsp; a list.&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="want.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/109132i6354D25C3BB09A51/image-size/medium?v=v2&amp;amp;px=400" role="button" title="want.png" alt="want.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Aug 2025 10:30:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-Variable-Header-for-Lowest-Value/m-p/972963#M377614</guid>
      <dc:creator>Mubarak_Basha</dc:creator>
      <dc:date>2025-08-20T10:30:29Z</dc:date>
    </item>
  </channel>
</rss>

