<?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 proc sql or other ways to select top 2 or more if there are more items sharing the 2nd highest value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-or-other-ways-to-select-top-2-or-more-if-there-are-more/m-p/952819#M372357</link>
    <description>&lt;P&gt;I would select the top 2 items for two groups. When the 2nd item is equal to the 3rd and 4th I'd like those included. The data is shown below.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID      Count	
A	80
A	25
A	25
A	25
A	20
B	70
B	30
B	10
B	9
B	7
&lt;/PRE&gt;
&lt;P&gt;It would be&lt;/P&gt;
&lt;PRE&gt;ID      Count	
A	80
A	25
A	25
A	25
B	70
B	30
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 07 Dec 2024 02:54:08 GMT</pubDate>
    <dc:creator>SeaMoon_168</dc:creator>
    <dc:date>2024-12-07T02:54:08Z</dc:date>
    <item>
      <title>proc sql or other ways to select top 2 or more if there are more items sharing the 2nd highest value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-or-other-ways-to-select-top-2-or-more-if-there-are-more/m-p/952819#M372357</link>
      <description>&lt;P&gt;I would select the top 2 items for two groups. When the 2nd item is equal to the 3rd and 4th I'd like those included. The data is shown below.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID      Count	
A	80
A	25
A	25
A	25
A	20
B	70
B	30
B	10
B	9
B	7
&lt;/PRE&gt;
&lt;P&gt;It would be&lt;/P&gt;
&lt;PRE&gt;ID      Count	
A	80
A	25
A	25
A	25
B	70
B	30
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 07 Dec 2024 02:54:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-or-other-ways-to-select-top-2-or-more-if-there-are-more/m-p/952819#M372357</guid>
      <dc:creator>SeaMoon_168</dc:creator>
      <dc:date>2024-12-07T02:54:08Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql or other ways to select top 2 or more if there are more items sharing the 2nd highest v</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-or-other-ways-to-select-top-2-or-more-if-there-are-more/m-p/952820#M372358</link>
      <description>&lt;P&gt;That is what PROC RANK is for.&amp;nbsp; You want the TIES=DENSE option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First let's convert your listing back into an actual dataset so we have something to program against.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID $ Count;
cards;
A 80
B 25
C 25
D 25
E 20
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you make a new RANK variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc rank data=have out=want ties=dense ;
  var count;
  ranks rank ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could then subset the results later&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc print data=want;
  where rank &amp;lt;= 2 ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or if you know you don't care about the other ranks subset it on the way out of the PROC RANK step by using the WHERE= dataset option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;... out=want(where=(rand&amp;lt;=2)) ...&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 07 Dec 2024 02:37:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-or-other-ways-to-select-top-2-or-more-if-there-are-more/m-p/952820#M372358</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-12-07T02:37:27Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql or other ways to select top 2 or more if there are more items sharing the 2nd highest v</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-or-other-ways-to-select-top-2-or-more-if-there-are-more/m-p/952825#M372363</link>
      <description>&lt;P&gt;Here is PROC SQL solution if you want it and your dataset is not big.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID  $    Count	;
cards;
A	80
A	25
A	25
A	25
A	20
B	70
B	30
B	10
B	9
B	7
;

proc sql;
create table want as
select * from have as c where exists(select * from
(
select a.id,a.count
 from have as a,have as b
  where a.id=b.id and a.count&amp;lt;=b.count 
   group by a.id,a.count
    having count(distinct b.count)&amp;lt;3
) as d
where c.id=d.id and c.count=d.count
)
order by 1,2 desc;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 07 Dec 2024 07:07:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-or-other-ways-to-select-top-2-or-more-if-there-are-more/m-p/952825#M372363</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-12-07T07:07:16Z</dc:date>
    </item>
  </channel>
</rss>

