<?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: Select top 3 records in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-top-3-records/m-p/451548#M113865</link>
    <description>&lt;P&gt;2 more ways to do this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By Plain SQL&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table have(drop = rank) as 
select id, sal,
(select count(distinct sal) from emp b
where a.sal&amp;lt;=b.sal)as rank
from emp a
where calculated rank le 3
order by sal;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;By using proc rank and using the&amp;nbsp;rank&amp;nbsp;&amp;nbsp;and then sort&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc rank data= emp out=emp_rank descending  ties=dense;
var sal;
   ranks sal_rank;
run;



proc sort data=emp_rank(where =(sal_rank le 3)) out=want(drop=sal_rank);
by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 05 Apr 2018 15:01:23 GMT</pubDate>
    <dc:creator>kiranv_</dc:creator>
    <dc:date>2018-04-05T15:01:23Z</dc:date>
    <item>
      <title>Select top 3 records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-top-3-records/m-p/451505#M113845</link>
      <description>&lt;P&gt;HI&amp;nbsp; ,&lt;/P&gt;&lt;P&gt;i have data set like below , where the employee id is repeating and the salary for the repeated id is also same&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i want to take top 3 salary from the table , and if the id is repeated then i want all the repeated rows also e.g emp 1 and 2 is repeated in original table so in result also they are repeated .&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data emp;&lt;BR /&gt;input id sal;&lt;BR /&gt;cards;&lt;BR /&gt;1 10000&lt;BR /&gt;2 20000&lt;BR /&gt;3 30000&lt;BR /&gt;1 10000&lt;BR /&gt;2 20000&lt;BR /&gt;4 5000&lt;BR /&gt;5 4000&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;data want&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;SAL&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;10000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;10000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;20000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;20000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;30000&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 13:45:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-top-3-records/m-p/451505#M113845</guid>
      <dc:creator>soham_sas</dc:creator>
      <dc:date>2018-04-05T13:45:41Z</dc:date>
    </item>
    <item>
      <title>Re: Select top 3 records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-top-3-records/m-p/451522#M113848</link>
      <description>&lt;P&gt;One way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have;&lt;/P&gt;
&lt;P&gt;by descending sal id;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by id notsorted;&lt;/P&gt;
&lt;P&gt;output;&lt;/P&gt;
&lt;P&gt;if last.id then count + 1;&lt;/P&gt;
&lt;P&gt;if count=3 then stop;&lt;/P&gt;
&lt;P&gt;drop count;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Optionally, you might want to re-sort:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=want;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 14:05:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-top-3-records/m-p/451522#M113848</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-04-05T14:05:02Z</dc:date>
    </item>
    <item>
      <title>Re: Select top 3 records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-top-3-records/m-p/451548#M113865</link>
      <description>&lt;P&gt;2 more ways to do this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By Plain SQL&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table have(drop = rank) as 
select id, sal,
(select count(distinct sal) from emp b
where a.sal&amp;lt;=b.sal)as rank
from emp a
where calculated rank le 3
order by sal;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;By using proc rank and using the&amp;nbsp;rank&amp;nbsp;&amp;nbsp;and then sort&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc rank data= emp out=emp_rank descending  ties=dense;
var sal;
   ranks sal_rank;
run;



proc sort data=emp_rank(where =(sal_rank le 3)) out=want(drop=sal_rank);
by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Apr 2018 15:01:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-top-3-records/m-p/451548#M113865</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2018-04-05T15:01:23Z</dc:date>
    </item>
    <item>
      <title>Re: Select top 3 records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-top-3-records/m-p/451658#M113910</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data emp;
input id sal;
cards;
1 10000
2 20000
3 30000
1 10000
2 20000
4 5000
5 4000
;
run;

data want;
if _n_=1 then do;
if 0 then set emp;
   dcl hash H (dataset:'emp(obs=3)',ordered: "d") ;
   h.definekey  ("sal") ;
   h.definedone () ;
end;
set emp;
if h.find()=0 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 18:59:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-top-3-records/m-p/451658#M113910</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-04-05T18:59:25Z</dc:date>
    </item>
  </channel>
</rss>

