<?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: Nth maximum value with same values  using datastep in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Nth-maximum-value-with-same-values-using-datastep/m-p/890139#M351723</link>
    <description>&lt;P&gt;If you're willing to read the dataset twice in a single data step, then the simplest code would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set sashelp.class (in=first_pass)  sashelp.class (in=second_pass);
  array _age{2} _temporary_;
  if first_pass=1 and age&amp;gt;_age{1} then do;
    _age{1}=age;
    call sortn(of _age{*});   /* Results in ascending sort of _age{1} and _age{2} */
  end;
  if second_pass and age=_age{1};
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 21 Aug 2023 03:32:35 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2023-08-21T03:32:35Z</dc:date>
    <item>
      <title>Nth maximum value with same values  using datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nth-maximum-value-with-same-values-using-datastep/m-p/890047#M351670</link>
      <description>&lt;P&gt;Hi Guys,&lt;/P&gt;
&lt;P&gt;I am trying Nth max value using datastep if same nth maxvalues in&amp;nbsp; row a dataset&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Using proc rank procedure nth maxvalues in a row */

proc rank
    data=sashelp.class
    ties=dense descending
    out=want(where=(number_rank=2))
    ;
  var age;
  ranks number_rank;
run;





data rank;
set sashelp.class;
run;

proc sort data=rank;
by descending age;
run;

data ranking;
set rank;
by  descending age ;
retain ranks;
if first.age then ranks+1;
proc print noobs;
run;


data maxcount ;
set ranking (obs=5);
if _n_ =2 then output; 
proc print noobs;
run;



proc rank
    data=sashelp.class
    ties=dense descending
    out=want(where=(number_rank=2))
    ;
  var age;
  ranks number_rank;
run;






&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want required output&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 288pt;" border="0" width="384" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" style="height: 15.0pt; width: 48pt;"&gt;
&lt;DIV style="font-size: 0.889rem; text-overflow: ellipsis;"&gt;Name
&lt;DIV style="cursor: col-resize; border-left: transparent; outline: none; text-overflow: ellipsis;"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;
&lt;DIV style="font-size: 0.889rem; text-overflow: ellipsis;"&gt;&amp;nbsp;&lt;/DIV&gt;
Sex
&lt;DIV style="cursor: col-resize; border-left: transparent; outline: none; text-overflow: ellipsis;"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;
&lt;DIV style="font-size: 0.889rem; text-overflow: ellipsis;"&gt;&amp;nbsp;&lt;/DIV&gt;
Age
&lt;DIV style="cursor: col-resize; border-left: transparent; outline: none; text-overflow: ellipsis;"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;
&lt;DIV style="font-size: 0.889rem; text-overflow: ellipsis;"&gt;&amp;nbsp;&lt;/DIV&gt;
Height
&lt;DIV style="cursor: col-resize; border-left: transparent; outline: none; text-overflow: ellipsis;"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;
&lt;DIV style="font-size: 0.889rem; text-overflow: ellipsis;"&gt;&amp;nbsp;&lt;/DIV&gt;
Weight
&lt;DIV style="cursor: col-resize; border-left: transparent; outline: none; text-overflow: ellipsis;"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;
&lt;DIV style="font-size: 0.889rem; text-overflow: ellipsis;"&gt;&amp;nbsp;&lt;/DIV&gt;
number_rank
&lt;DIV style="cursor: col-resize; border-left: transparent; outline: none; text-overflow: ellipsis;"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 1.5rem;"&gt;Janet&lt;/TD&gt;
&lt;TD style="height: 1.5rem;"&gt;F&lt;/TD&gt;
&lt;TD align="right" style="height: 1.5rem;"&gt;15&lt;/TD&gt;
&lt;TD align="right" style="height: 1.5rem;"&gt;62.5&lt;/TD&gt;
&lt;TD align="right" style="height: 1.5rem;"&gt;112.5&lt;/TD&gt;
&lt;TD align="right" style="height: 1.5rem;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 1.5rem;"&gt;Mary&lt;/TD&gt;
&lt;TD style="height: 1.5rem;"&gt;F&lt;/TD&gt;
&lt;TD align="right" style="height: 1.5rem;"&gt;15&lt;/TD&gt;
&lt;TD align="right" style="height: 1.5rem;"&gt;66.5&lt;/TD&gt;
&lt;TD align="right" style="height: 1.5rem;"&gt;112&lt;/TD&gt;
&lt;TD align="right" style="height: 1.5rem;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 1.5rem;"&gt;Ronald&lt;/TD&gt;
&lt;TD style="height: 1.5rem;"&gt;M&lt;/TD&gt;
&lt;TD align="right" style="height: 1.5rem;"&gt;15&lt;/TD&gt;
&lt;TD align="right" style="height: 1.5rem;"&gt;67&lt;/TD&gt;
&lt;TD align="right" style="height: 1.5rem;"&gt;133&lt;/TD&gt;
&lt;TD align="right" style="height: 1.5rem;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 1.5rem;"&gt;William&lt;/TD&gt;
&lt;TD style="height: 1.5rem;"&gt;M&lt;/TD&gt;
&lt;TD align="right" style="height: 1.5rem;"&gt;15&lt;/TD&gt;
&lt;TD align="right" style="height: 1.5rem;"&gt;66.5&lt;/TD&gt;
&lt;TD align="right" style="height: 1.5rem;"&gt;112&lt;/TD&gt;
&lt;TD align="right" style="height: 1.5rem;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Aug 2023 11:24:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nth-maximum-value-with-same-values-using-datastep/m-p/890047#M351670</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2023-08-19T11:24:40Z</dc:date>
    </item>
    <item>
      <title>Re: Nth maximum value with same values  using datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nth-maximum-value-with-same-values-using-datastep/m-p/890051#M351673</link>
      <description>&lt;P&gt;For efficiency, combine the first two steps:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort
  data=sashelp.class
  out=rank
;
by descending age;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then, define the ranks:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set rank;
by descending age;
rank + (first.age); * SUM statement implies RETAIN;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I leave it as a simple exercise to filter for rank = 2 in the second step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: added the DESCENDING keyword in the DATA step.&lt;/P&gt;</description>
      <pubDate>Sun, 20 Aug 2023 07:54:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nth-maximum-value-with-same-values-using-datastep/m-p/890051#M351673</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-08-20T07:54:19Z</dc:date>
    </item>
    <item>
      <title>Re: Nth maximum value with same values  using datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nth-maximum-value-with-same-values-using-datastep/m-p/890077#M351686</link>
      <description>&lt;P&gt;In this case you merely need to keep a running record of the top 2 values of AGE as you read sashelp.class.&amp;nbsp; Every time an obs is encountered that matches the Nth largest age (2nd largest), stick it in a hash object.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you reach the end of sashelp.class, you know the Nth largest age value, and you can use that to output data from the hash object, filtering on age=Nth highest value.&amp;nbsp; Given the hash object is declared to be sorted by the same var (name) as sashelp.class, the resulting dataset has the original data order:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set sashelp.class end=end_of_class;
  retain age1 age2 ;

  if _n_=1 then do;
    declare hash h (dataset:'sashelp.class (obs=0)',ordered:'A');
      h.definekey('name');
      h.definedata(all:'Y');
      h.definedone();
  end;

  if age&amp;gt;=age2 then h.add();
  if age&amp;gt;age1 then do; age2=age1; age1=age; end; else
  if age&amp;gt;age2 then     age2=age;

  if end_of_class then h.output(dataset:cats('want (where=(age=',age2,'))'));

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Aug 2023 23:49:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nth-maximum-value-with-same-values-using-datastep/m-p/890077#M351686</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-08-19T23:49:04Z</dc:date>
    </item>
    <item>
      <title>Re: Nth maximum value with same values  using datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nth-maximum-value-with-same-values-using-datastep/m-p/890139#M351723</link>
      <description>&lt;P&gt;If you're willing to read the dataset twice in a single data step, then the simplest code would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set sashelp.class (in=first_pass)  sashelp.class (in=second_pass);
  array _age{2} _temporary_;
  if first_pass=1 and age&amp;gt;_age{1} then do;
    _age{1}=age;
    call sortn(of _age{*});   /* Results in ascending sort of _age{1} and _age{2} */
  end;
  if second_pass and age=_age{1};
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Aug 2023 03:32:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nth-maximum-value-with-same-values-using-datastep/m-p/890139#M351723</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-08-21T03:32:35Z</dc:date>
    </item>
    <item>
      <title>Re: Nth maximum value with same values  using datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nth-maximum-value-with-same-values-using-datastep/m-p/890144#M351724</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;proc sort
  data=sashelp.class
  out=rank
;
by descending age;
run;

data want;
set rank ;
by descending age;
rank + (first.age); * SUM statement implies RETAIN;
if rank=2 ; /*fIlter Nth rank using if statement Note  where statement cannot work here because assingnment variable rank only work with IF statement*/
proc print noobs;
run&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;Kurt&amp;nbsp; for your solution&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Aug 2023 04:59:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nth-maximum-value-with-same-values-using-datastep/m-p/890144#M351724</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2023-08-21T04:59:47Z</dc:date>
    </item>
    <item>
      <title>Re: Nth maximum value with same values  using datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nth-maximum-value-with-same-values-using-datastep/m-p/890145#M351725</link>
      <description>&lt;P&gt;Thank You,&lt;/P&gt;
&lt;P&gt;Mkeintz for your solution&amp;nbsp;&lt;/P&gt;
&lt;P&gt;in your tricky code how to filter Nth max values&amp;nbsp; suppose i want 5 max value where i have to change in this code&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Aug 2023 05:02:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nth-maximum-value-with-same-values-using-datastep/m-p/890145#M351725</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2023-08-21T05:02:33Z</dc:date>
    </item>
    <item>
      <title>Re: Nth maximum value with same values  using datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nth-maximum-value-with-same-values-using-datastep/m-p/891209#M352096</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265860"&gt;@BrahmanandaRao&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank You,&lt;/P&gt;
&lt;P&gt;Mkeintz for your solution&amp;nbsp;&lt;/P&gt;
&lt;P&gt;in your tricky code how to filter Nth max values&amp;nbsp; suppose i want 5 max value where i have to change in this code&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you want the 5th largest age, or all observations with the 5th largest or larger?&lt;/P&gt;</description>
      <pubDate>Sun, 27 Aug 2023 01:54:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nth-maximum-value-with-same-values-using-datastep/m-p/891209#M352096</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-08-27T01:54:14Z</dc:date>
    </item>
  </channel>
</rss>

