<?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: Find nth highest value within each group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-nth-highest-value-within-each-group/m-p/487633#M287309</link>
    <description>&lt;P&gt;Why monotonic()?&lt;/P&gt;
&lt;P&gt;SQL is just not good at&amp;nbsp;considering&amp;nbsp;rows in order, unless ORDER BY is used.&lt;/P&gt;
&lt;P&gt;This does what you want:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select a.ID, a.SAL, count(*) as ORDER 
  from HAVE a
      ,HAVE b
  where a.ID=b.ID and a.SAL&amp;lt;=b.SAL
  group by 1,2
  having ORDER=3;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;TABLE style="border-collapse: collapse; width: 144pt;" border="0" width="192" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" class="xl66" style="height: 15.0pt; width: 48pt;"&gt;ID&lt;/TD&gt;
&lt;TD width="64" class="xl67" style="width: 48pt;"&gt;SAL&lt;/TD&gt;
&lt;TD width="64" class="xl67" style="width: 48pt;"&gt;ORDER&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" align="right" class="xl68" style="height: 15.0pt; width: 48pt;"&gt;1&lt;/TD&gt;
&lt;TD width="64" align="right" class="xl65" style="width: 48pt;"&gt;20&lt;/TD&gt;
&lt;TD width="64" align="right" class="xl65" style="width: 48pt;"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" align="right" class="xl68" style="height: 15.0pt; width: 48pt;"&gt;2&lt;/TD&gt;
&lt;TD width="64" align="right" class="xl65" style="width: 48pt;"&gt;4&lt;/TD&gt;
&lt;TD width="64" align="right" class="xl65" style="width: 48pt;"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 17 Aug 2018 05:13:45 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2018-08-17T05:13:45Z</dc:date>
    <item>
      <title>Find nth highest value within each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-nth-highest-value-within-each-group/m-p/487632#M287308</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input id sal;
cards;
1 10
1 20 
1 40 
1 35 
1 5
2 3 
2 6
2 4 
2 78
;
run;

* I want within each distinct group of id I want 3rd largest sal value using monotonic();&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Aug 2018 04:44:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-nth-highest-value-within-each-group/m-p/487632#M287308</guid>
      <dc:creator>Arun_shSAS</dc:creator>
      <dc:date>2018-08-17T04:44:13Z</dc:date>
    </item>
    <item>
      <title>Re: Find nth highest value within each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-nth-highest-value-within-each-group/m-p/487633#M287309</link>
      <description>&lt;P&gt;Why monotonic()?&lt;/P&gt;
&lt;P&gt;SQL is just not good at&amp;nbsp;considering&amp;nbsp;rows in order, unless ORDER BY is used.&lt;/P&gt;
&lt;P&gt;This does what you want:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select a.ID, a.SAL, count(*) as ORDER 
  from HAVE a
      ,HAVE b
  where a.ID=b.ID and a.SAL&amp;lt;=b.SAL
  group by 1,2
  having ORDER=3;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;TABLE style="border-collapse: collapse; width: 144pt;" border="0" width="192" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" class="xl66" style="height: 15.0pt; width: 48pt;"&gt;ID&lt;/TD&gt;
&lt;TD width="64" class="xl67" style="width: 48pt;"&gt;SAL&lt;/TD&gt;
&lt;TD width="64" class="xl67" style="width: 48pt;"&gt;ORDER&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" align="right" class="xl68" style="height: 15.0pt; width: 48pt;"&gt;1&lt;/TD&gt;
&lt;TD width="64" align="right" class="xl65" style="width: 48pt;"&gt;20&lt;/TD&gt;
&lt;TD width="64" align="right" class="xl65" style="width: 48pt;"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" align="right" class="xl68" style="height: 15.0pt; width: 48pt;"&gt;2&lt;/TD&gt;
&lt;TD width="64" align="right" class="xl65" style="width: 48pt;"&gt;4&lt;/TD&gt;
&lt;TD width="64" align="right" class="xl65" style="width: 48pt;"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Aug 2018 05:13:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-nth-highest-value-within-each-group/m-p/487633#M287309</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-08-17T05:13:45Z</dc:date>
    </item>
    <item>
      <title>Re: Find nth highest value within each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-nth-highest-value-within-each-group/m-p/487637#M287310</link>
      <description>&lt;P&gt;If you want a Data Step Solution ...&lt;/P&gt;
&lt;P&gt;Data Set A is assumed to be sorted by ID. Assumed that within ID there will not be&amp;nbsp; more than 100 SAL values. Used Array to store SALs for each ID. Use LARGEST function to get your Kth ORDERED Value from the Array. Clean the Array and process the next ID as above.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input id sal;
cards;
1 10
1 20 
1 40 
1 35 
1 5
2 3 
2 6
2 4 
2 78
;
run;

data want;
   array k[100] _temporary_;
   do i = 1 by 1 until(last.id);
      set a;
      by id notsorted;
      k[i] = sal;
   end;
   third_Largest = Largest(3, of k[*]);
   put third_largest =;
   call missing(of k[*]);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Cheers.&lt;/P&gt;
&lt;P&gt;DATASP&lt;/P&gt;</description>
      <pubDate>Fri, 17 Aug 2018 06:08:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-nth-highest-value-within-each-group/m-p/487637#M287310</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2018-08-17T06:08:07Z</dc:date>
    </item>
    <item>
      <title>Re: Find nth highest value within each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-nth-highest-value-within-each-group/m-p/487655#M287311</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/91892"&gt;@Arun_shSAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input id sal;
cards;
1 10
1 20 
1 40 
1 35 
1 5
2 3 
2 6
2 4 
2 78
;
run;

* I want within each distinct group of id I want 3rd largest sal value using monotonic();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Basic data step solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=a;
by id descending sal;
run;

data want;
set a;
by id;
if first.id
then count = 1;
else count + 1;
if count = 3;
drop count;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Aug 2018 07:47:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-nth-highest-value-within-each-group/m-p/487655#M287311</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-08-17T07:47:29Z</dc:date>
    </item>
    <item>
      <title>Re: Find nth highest value within each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-nth-highest-value-within-each-group/m-p/487656#M287312</link>
      <description>&lt;PRE&gt;%let nth=3;

data a;
  input id sal;
cards;
1 10
1 20 
1 40 
1 35 
1 5
2 3 
2 6
2 4 
2 78
;
run;

proc sort data=a;
  by id descending sal;
run;

data want;
  set a;
  retain cnt;
  by id;
  cnt=ifn(first.id,1,cnt+1);
  if cnt=&amp;amp;nth.;
run;&lt;/PRE&gt;
&lt;P&gt;Combine that with any other processing.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Aug 2018 07:50:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-nth-highest-value-within-each-group/m-p/487656#M287312</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-08-17T07:50:26Z</dc:date>
    </item>
    <item>
      <title>Re: Find nth highest value within each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-nth-highest-value-within-each-group/m-p/487672#M287313</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/91892"&gt;@Arun_shSAS&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;monotonic() is an undocumented and unsupported function which you shouldn't use at all.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Aug 2018 08:50:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-nth-highest-value-within-each-group/m-p/487672#M287313</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-08-17T08:50:07Z</dc:date>
    </item>
    <item>
      <title>Re: Find nth highest value within each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-nth-highest-value-within-each-group/m-p/488038#M287314</link>
      <description>&lt;P&gt;You might use monotonic() to examine some of the behavior of sql, but I wouldn't recommend it for work you rely on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Neither sql nor the data step is the most efficacious solution.&amp;nbsp; I'd recommend proc rank (assuming data are sorted by id):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc rank data=have out=want (where=(salrank=3)) descending;
  by id;
  var sal;
  ranks salrank;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the data were already sorted by descending salary within id, then the data step would be easiest.&amp;nbsp; But if the within-id records have any other order, proc rank is a very direct solution.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Aug 2018 02:55:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-nth-highest-value-within-each-group/m-p/488038#M287314</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-08-19T02:55:28Z</dc:date>
    </item>
  </channel>
</rss>

