<?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 function.  which variable/argument contained the max value? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/max-function-which-variable-argument-contained-the-max-value/m-p/913298#M359979</link>
    <description>&lt;P&gt;Yes. I think is easiest if you put the variables into an array and &lt;A href="https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html" target="_self"&gt;use the OF keyword&lt;/A&gt;&amp;nbsp;to supply the values to the MAX function, like this:&amp;nbsp; MAX(of arrayName[*}) .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway, the function you want is the WHICHN function, which returns the (first) index of a list a variables that equals a specified value.&lt;/P&gt;
&lt;P&gt;So first compute the max, then use WHICHN to find out which variables (1st, 2nd, 3rd, etc) it matches. If necessary, you can use the VNAME function to get the name of the variable:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* use OP's data */
data TempMax;
set TempMax;
array myVars[*] A B C D E;  /* or A--E if contiguous */
length varName $32;
date1 = max(A,B,C,D,E);       /* OP's current code */
maxDate = max(of myVars[*]);  /* an equivalent computation that uses arrays */
/* WHICHN function: https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0zs0pv38mel2jn1in4lte2akx4d.htm */
Index = whichn(maxDate, of myVars[*]);
/* VARNAME function: https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n1cjm0xdeczcd5n1s7d8ah9x8432.htm */
varName = vname(myVars[Index]);
run;


proc print data=TempMax; 
   var ID A B C D E maxDate Index varName;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 29 Jan 2024 01:15:03 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2024-01-29T01:15:03Z</dc:date>
    <item>
      <title>max function.  which variable/argument contained the max value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-function-which-variable-argument-contained-the-max-value/m-p/913290#M359975</link>
      <description>&lt;P&gt;Given a table such as:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data TempMax;
    input ID A B C D E;
    datalines;
        1 234 432 245 555 546
        2 334 258 593 599 113
        3 948 874 737 333 388
        ;
    run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Obs    ID     A      B      C      D      E

 1      1    234    432    245    555    546
 2      2    334    258    593    599    113
 3      3    948    874    737    333    388
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and performing a max function on it&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data TempMax;
        set TempMax;
            latestdate = max(A,B,C,D,E);
        run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to give&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Obs    ID     A      B      C      D      E     latestdate

 1      1    234    432    245    555    546        555   
 2      2    334    258    593    599    113        599   
 3      3    948    874    737    333    388        948   
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way to determine which variable was the max?&amp;nbsp; either by identifying the variable, or its argument position in the max function?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, the values in the dataset I am working with are dates. and no 2 dates in an obs are equal.&amp;nbsp; so there is one and only one argument to the max function that will be the max.&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>Mon, 29 Jan 2024 00:14:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-function-which-variable-argument-contained-the-max-value/m-p/913290#M359975</guid>
      <dc:creator>mcook</dc:creator>
      <dc:date>2024-01-29T00:14:35Z</dc:date>
    </item>
    <item>
      <title>Re: max function.  which variable/argument contained the max value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-function-which-variable-argument-contained-the-max-value/m-p/913298#M359979</link>
      <description>&lt;P&gt;Yes. I think is easiest if you put the variables into an array and &lt;A href="https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html" target="_self"&gt;use the OF keyword&lt;/A&gt;&amp;nbsp;to supply the values to the MAX function, like this:&amp;nbsp; MAX(of arrayName[*}) .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway, the function you want is the WHICHN function, which returns the (first) index of a list a variables that equals a specified value.&lt;/P&gt;
&lt;P&gt;So first compute the max, then use WHICHN to find out which variables (1st, 2nd, 3rd, etc) it matches. If necessary, you can use the VNAME function to get the name of the variable:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* use OP's data */
data TempMax;
set TempMax;
array myVars[*] A B C D E;  /* or A--E if contiguous */
length varName $32;
date1 = max(A,B,C,D,E);       /* OP's current code */
maxDate = max(of myVars[*]);  /* an equivalent computation that uses arrays */
/* WHICHN function: https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0zs0pv38mel2jn1in4lte2akx4d.htm */
Index = whichn(maxDate, of myVars[*]);
/* VARNAME function: https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n1cjm0xdeczcd5n1s7d8ah9x8432.htm */
varName = vname(myVars[Index]);
run;


proc print data=TempMax; 
   var ID A B C D E maxDate Index varName;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jan 2024 01:15:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-function-which-variable-argument-contained-the-max-value/m-p/913298#M359979</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2024-01-29T01:15:03Z</dc:date>
    </item>
    <item>
      <title>Re: max function.  which variable/argument contained the max value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-function-which-variable-argument-contained-the-max-value/m-p/913372#M360012</link>
      <description>&lt;P&gt;I am a bit confused as to why you are calling your maximum value "latestdate". If your values are actually dates then use dates for your example. When you show values like 234 it makes one suspect that you may not have actual SAS date values and will have other problems later.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data TempMax;
    input ID  A :date9. B :date9. C :date9. D :date9. E :date9. ;
    format a b c d e date9.;
datalines;
1 01JAN2019 03FEB2019 04AUG2020 11NOV2021 12DEC2022
2 15DEC2022 10OCT2022 08AUG2021 06JUN2018 04APR2019
3 11OCT2023 08MAR2018 09FEB2022 22JAN2021 30MAR2017
;
  &lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jan 2024 11:50:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-function-which-variable-argument-contained-the-max-value/m-p/913372#M360012</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-01-29T11:50:06Z</dc:date>
    </item>
    <item>
      <title>Re: max function.  which variable/argument contained the max value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-function-which-variable-argument-contained-the-max-value/m-p/913382#M360020</link>
      <description>&lt;P&gt;Alternatively, start with a "long" dataset or create one from your existing data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=TempMax out=long;
by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can use PROC SUMMARY (or PROC SQL) to obtain the desired output quite naturally:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=long;
by id;
var col1;
id _name_;
output out=want(drop=_:) max=maxdate maxid=varname;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jan 2024 12:54:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-function-which-variable-argument-contained-the-max-value/m-p/913382#M360020</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-01-29T12:54:21Z</dc:date>
    </item>
  </channel>
</rss>

