<?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: For each variable in the SAS table I want to derive nth maximum value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774169#M246008</link>
    <description>Thank you so much! The code worked</description>
    <pubDate>Thu, 14 Oct 2021 10:31:27 GMT</pubDate>
    <dc:creator>Saurabh_Rana</dc:creator>
    <dc:date>2021-10-14T10:31:27Z</dc:date>
    <item>
      <title>For each variable in the SAS table I want to derive nth maximum value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774134#M245988</link>
      <description>&lt;P&gt;I have a SAS table with more than 100 columns like this-&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; C&amp;nbsp; &amp;nbsp; &amp;nbsp;D&amp;nbsp; &amp;nbsp; &amp;nbsp;E&amp;nbsp; &amp;nbsp; &amp;nbsp; F&lt;/P&gt;
&lt;P&gt;8&amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; 7&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;
&lt;P&gt;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I want to create a new table where against each variable nth maximum value is mentioned like this-&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Variable Name &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;nth Max Value&lt;/P&gt;
&lt;P&gt;A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;21&lt;/P&gt;
&lt;P&gt;B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;45&lt;/P&gt;
&lt;P&gt;C &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;32&lt;/P&gt;
&lt;P&gt;D &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;64&lt;/P&gt;
&lt;P&gt;E &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;13&lt;/P&gt;
&lt;P&gt;. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I derive such a table?&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Oct 2021 09:25:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774134#M245988</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2021-10-14T09:25:37Z</dc:date>
    </item>
    <item>
      <title>Re: For each variable in the SAS table I want to derive nth maximum value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774135#M245989</link>
      <description>&lt;P&gt;Do you want just 1 column like 'Nth max variable' ?&lt;/P&gt;</description>
      <pubDate>Thu, 14 Oct 2021 08:14:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774135#M245989</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-10-14T08:14:50Z</dc:date>
    </item>
    <item>
      <title>Re: For each variable in the SAS table I want to derive nth maximum value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774143#M245997</link>
      <description>&lt;P&gt;I want two columns, in the first column, all the variables names will be stored and in the second column, the nth max value for each variable (In the first column) will be stored.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Oct 2021 09:31:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774143#M245997</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2021-10-14T09:31:02Z</dc:date>
    </item>
    <item>
      <title>Re: For each variable in the SAS table I want to derive nth maximum value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774144#M245998</link>
      <description>&lt;P&gt;Like that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data  have;
  input A B C D E F;
cards;
8 3 7 1 3 3
23 4 5 267 7 84 2
0 11 4 6 9 11
;
run;

proc transpose data = have out = have1;
  var A--F;
run;

data want;
  set have1;
  array _C_{*} col:;
  n = 2; /* select second largest */
  nthValue = LARGEST(n, of _C_[*]);
  keep _NAME_ nthValue;
run;
proc print data = want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 14 Oct 2021 09:38:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774144#M245998</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-10-14T09:38:24Z</dc:date>
    </item>
    <item>
      <title>Re: For each variable in the SAS table I want to derive nth maximum value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774147#M246001</link>
      <description>&lt;P&gt;IML solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
DATA TEST;

FORMAT VAR1CHAR $3.;
FLAG=0;
DO I=1 TO 100;
IF 1=1 THEN DO;
var1char = COMPRESS(CATX('', IFC(^FLAG, byte(I+64), CATX('', BYTE(64 + FLAG), byte(I+64 + IFN(FLAG=0, 0, -FLAG*26)))))) ;
FLAG=(MOD(I,26)=0)+FLAG;
OUTPUT;
END;
END;

RUN; 

proc transpose data=test out=want1( drop=_name_);
id var1char;
var flag;
run;

data want;
set want1;
format temp2 $500.;
array temp {100} _temporary_ (1 : 100);
array filler {100} _numeric_;
s = 123;
do _N_ = 1 to 10;
      call ranperm(s, of temp [*]);
temp2 = catq('d', '|', of temp [*]);
do port=1 to 100;
filler(port)=input(scan(temp2, port,  "|"), best12.);
end;
output;
end;
drop port temp2;
run;

proc iml;
use want;
read all var _all_ into x [colname=varname];
close;

maxy=x [&amp;lt;&amp;gt;,];

print maxy [c=varname];&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Oct 2021 09:42:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774147#M246001</guid>
      <dc:creator>acordes</dc:creator>
      <dc:date>2021-10-14T09:42:38Z</dc:date>
    </item>
    <item>
      <title>Re: For each variable in the SAS table I want to derive nth maximum value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774161#M246004</link>
      <description>In proc transpose step how can I select all variables in the table</description>
      <pubDate>Thu, 14 Oct 2021 10:08:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774161#M246004</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2021-10-14T10:08:22Z</dc:date>
    </item>
    <item>
      <title>Re: For each variable in the SAS table I want to derive nth maximum value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774163#M246005</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;var _all_;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Oct 2021 10:09:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774163#M246005</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-10-14T10:09:54Z</dc:date>
    </item>
    <item>
      <title>Re: For each variable in the SAS table I want to derive nth maximum value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774169#M246008</link>
      <description>Thank you so much! The code worked</description>
      <pubDate>Thu, 14 Oct 2021 10:31:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774169#M246008</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2021-10-14T10:31:27Z</dc:date>
    </item>
    <item>
      <title>Re: For each variable in the SAS table I want to derive nth maximum value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774170#M246009</link>
      <description>&lt;P&gt;Just for fun in IML&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
use have1;
read all var _num_ into x;
read all var {"_name_"} into vary;
close;

nth_highest=2;
nth_maxy=j(nrow(x),1,.);
nth_maxy_alt=nth_maxy;
do i=1 to nrow(x);
x_temp=x[i,]`;
call sort(x_temp,1,1);
nth_maxy_alt[i]=x_temp[nth_highest];
nth_maxy[i]=setdif(x[i,], x[i,&amp;lt;&amp;gt;]) [,&amp;lt;&amp;gt;]; /*works for the second highest*/
end;

print x nth_maxy nth_maxy_alt vary;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="a1.png" style="width: 469px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64698iECB95F990D054B80/image-size/large?v=v2&amp;amp;px=999" role="button" title="a1.png" alt="a1.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Oct 2021 10:31:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774170#M246009</guid>
      <dc:creator>acordes</dc:creator>
      <dc:date>2021-10-14T10:31:40Z</dc:date>
    </item>
    <item>
      <title>Re: For each variable in the SAS table I want to derive nth maximum value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774171#M246010</link>
      <description>If there are repeated numbers in the column then do they get counted or not?</description>
      <pubDate>Thu, 14 Oct 2021 10:32:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774171#M246010</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2021-10-14T10:32:23Z</dc:date>
    </item>
    <item>
      <title>Re: For each variable in the SAS table I want to derive nth maximum value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774177#M246013</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data  have;
  input A B C D E F;
cards;
8 3 7 1 3 3
23 4 5 267 7 84 2
0 11 4 6 9 11
;

data want_hoh(keep = v n);

    set have end = z;
    array nn _numeric_;

    if _N_ = 1 then do;
        dcl hash hh ();
        hh.definekey ("_i_");
        hh.definedata ("h", "i");
        hh.definedone ();
        
        do over nn;
            n = nn;
            dcl hash h (multidata : "Y", ordered : "D");
            h.definekey ("n");
            h.definedone ();
            dcl hiter i("h");
            hh.add();
        end;
    end;
    
    do over nn;
        hh.find();
        h.add (key:nn, data:nn);
    end;
    
    if z then do over nn;
        v = vname(nn);
        hh.find();
        
        do _N_ = 1 by 1 while (i.next() = 0 &amp;amp; _N_ &amp;lt; 2);
        end;
        
        output;
        
        rc = i.last();
        rc = i.next();
    end;
    
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Oct 2021 11:12:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774177#M246013</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-10-14T11:12:34Z</dc:date>
    </item>
    <item>
      <title>Re: For each variable in the SAS table I want to derive nth maximum value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774178#M246014</link>
      <description>&lt;P&gt;The problem is simple if you use PROC RANK. In addition, unlike the other methods presented, PROC RANK gives you options on how to handle ties.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Oct 2021 11:15:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774178#M246014</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-10-14T11:15:39Z</dc:date>
    </item>
    <item>
      <title>Re: For each variable in the SAS table I want to derive nth maximum value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774179#M246015</link>
      <description>Can you please share sample code</description>
      <pubDate>Thu, 14 Oct 2021 11:21:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774179#M246015</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2021-10-14T11:21:53Z</dc:date>
    </item>
    <item>
      <title>Re: For each variable in the SAS table I want to derive nth maximum value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774182#M246016</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/316224"&gt;@Saurabh_Rana&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Can you please share sample code&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Example:&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/proc/n18s1uw2tqvtxdn1chay1qq1jke6.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/proc/n18s1uw2tqvtxdn1chay1qq1jke6.htm&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Oct 2021 11:25:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-variable-in-the-SAS-table-I-want-to-derive-nth-maximum/m-p/774182#M246016</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-10-14T11:25:34Z</dc:date>
    </item>
  </channel>
</rss>

