<?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: How do I find top two records of dataset using proc SQl in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260929#M50683</link>
    <description>&lt;P&gt;&lt;STRONG&gt;Proc rank&lt;/STRONG&gt; can prove useful here&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc rank data=have descending ties=dense out=want(where=(rank&amp;lt;=2));
by cc;
var spend;
ranks rank;
run;

proc sort data=want(drop=rank) nodupkey; by cc descending spend; run;

proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 03 Apr 2016 01:38:28 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2016-04-03T01:38:28Z</dc:date>
    <item>
      <title>How do I find top two records of dataset using proc SQl</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260893#M50675</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;I was trying to find top two 2 records of my dataset using Proc SQl,&lt;/P&gt;
&lt;P&gt;so here is my data, I have Customer and spend variable, and I need to find what are top two&lt;/P&gt;
&lt;P&gt;spend each customer made?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data I have this:&lt;/P&gt;
&lt;P&gt;cc &amp;nbsp; &amp;nbsp; &amp;nbsp;spend&lt;BR /&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 100&lt;BR /&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 200&lt;BR /&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 550&lt;BR /&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;BR /&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;200&lt;BR /&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;550&lt;BR /&gt;1 &amp;nbsp; &amp;nbsp; 100&lt;BR /&gt;2 &amp;nbsp; &amp;nbsp; 200&lt;BR /&gt;2 &amp;nbsp; &amp;nbsp; 550&lt;BR /&gt;2 &amp;nbsp; &amp;nbsp;200&lt;BR /&gt;2 &amp;nbsp; &amp;nbsp;200&lt;BR /&gt;2 &amp;nbsp; &amp;nbsp;550&lt;BR /&gt;2 &amp;nbsp; &amp;nbsp;200&lt;BR /&gt;2 &amp;nbsp; &amp;nbsp;900&lt;BR /&gt;3 &amp;nbsp; &amp;nbsp;750&lt;BR /&gt;3 &amp;nbsp; &amp;nbsp;550&lt;BR /&gt;3 &amp;nbsp; &amp;nbsp;1300&lt;BR /&gt;3 &amp;nbsp; &amp;nbsp;1900&lt;BR /&gt;3 &amp;nbsp; &amp;nbsp;750&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried this but this is not giving me the correct results:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;Select CC,(select max(spend)from work.have&lt;BR /&gt;where spend NOT IN(select max(spend) from work.&lt;SPAN&gt;have&lt;/SPAN&gt;)) as Second_Max_spend from work.&lt;SPAN&gt;have&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;group by CC;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Expcted Results&lt;/P&gt;
&lt;P&gt;CC Spend&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; 550&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; 200&lt;/P&gt;
&lt;P&gt;2 &amp;nbsp; &amp;nbsp; 900&lt;/P&gt;
&lt;P&gt;2 &amp;nbsp; &amp;nbsp; 550&lt;/P&gt;
&lt;P&gt;3 &amp;nbsp; &amp;nbsp; 1900&lt;/P&gt;
&lt;P&gt;3 &amp;nbsp; &amp;nbsp; 1300&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Sat, 02 Apr 2016 13:18:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260893#M50675</guid>
      <dc:creator>LittlesasMaster</dc:creator>
      <dc:date>2016-04-02T13:18:21Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find top two records of dataset using proc SQl</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260896#M50676</link>
      <description>&lt;P&gt;Proc SQL has the OUTOBS option for this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql outobs=2;
select cc, max(spend) as max_spend
from work.have
group by cc
order by calculated max_spend desc
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;- Jan.&lt;/P&gt;</description>
      <pubDate>Sat, 02 Apr 2016 14:08:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260896#M50676</guid>
      <dc:creator>jklaverstijn</dc:creator>
      <dc:date>2016-04-02T14:08:46Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find top two records of dataset using proc SQl</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260901#M50678</link>
      <description>&lt;P&gt;Hey,&lt;/P&gt;
&lt;P&gt;It just did not help, expected result is something different , I need to figure out what are top&lt;/P&gt;
&lt;P&gt;two spend each customer made....(OUTBS is not the helping, I just dont' want only two Obs).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your reply&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;Dharmndra&lt;/P&gt;</description>
      <pubDate>Sat, 02 Apr 2016 16:00:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260901#M50678</guid>
      <dc:creator>LittlesasMaster</dc:creator>
      <dc:date>2016-04-02T16:00:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find top two records of dataset using proc SQl</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260903#M50679</link>
      <description>&lt;P&gt;It's easier if you use a data step here, rather than SQL. If you were doing it in SQL you would need an OVER clause that's not supported in SAS.&lt;/P&gt;
&lt;P&gt;Use the BY group feature to identify each group, create a counter for the observations and then ouptut the records that are the top 2.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by id descending spend;
run;

data want;
set have;
by id;
retain count;

if first.id then count=0;
count+1;

if count &amp;lt;=2 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Apr 2016 16:14:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260903#M50679</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-04-02T16:14:56Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find top two records of dataset using proc SQl</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260911#M50682</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/14488"&gt;@LittlesasMaster﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a pure PROC SQL approach:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create view top_one as
select cc, max(spend) as spend
from have
group by cc;

select * from top_one
union all
select cc, max(spend) as spend
from (select * from have except all select * from top_one)
group by cc
order by cc, spend desc;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please note that customer 1 has &lt;EM&gt;two&lt;/EM&gt; records with SPEND=550 and the above code selects these two. If you want to ignore duplicates in order to obtain what you posted as your "expected results" (i.e. SPEND=200 as the second highest amount for CC=1), simply delete the &lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;all&lt;/FONT&gt;&lt;/STRONG&gt; keyword in "&lt;FONT face="courier new,courier"&gt;except &lt;STRONG&gt;all&lt;/STRONG&gt;&lt;/FONT&gt;".&lt;/P&gt;</description>
      <pubDate>Sat, 02 Apr 2016 20:29:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260911#M50682</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-04-02T20:29:25Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find top two records of dataset using proc SQl</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260929#M50683</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Proc rank&lt;/STRONG&gt; can prove useful here&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc rank data=have descending ties=dense out=want(where=(rank&amp;lt;=2));
by cc;
var spend;
ranks rank;
run;

proc sort data=want(drop=rank) nodupkey; by cc descending spend; run;

proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 03 Apr 2016 01:38:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260929#M50683</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-04-03T01:38:28Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find top two records of dataset using proc SQl</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260932#M50684</link>
      <description>&lt;P&gt;Otherwise, use normal sort and BY processing or nodupkey sort and a simpler data step to get unique values&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=temp; by cc descending spend; run;

data want;
set temp; by cc descending spend;
if first.cc then count = 0;
if count &amp;lt; 2 then
    if first.spend then do;
        output;
        count + 1;
        end;
drop count;
run;

proc print; run;

/* Or... */
proc sort data=have out=temp2 nodupkey; by cc descending spend; run;

data want2;
set temp2; by cc;
if first.cc then count = 0;
if count &amp;lt; 2 then do;
    output;
    count + 1;
    end;
drop count;
run;

proc print; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 03 Apr 2016 01:55:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260932#M50684</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-04-03T01:55:18Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find top two records of dataset using proc SQl</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260933#M50685</link>
      <description>&lt;P&gt;But if you insist on SQL...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
select unique cc, spend
from have as a
where (select count(distinct spend) from have where cc=a.cc and spend &amp;gt; a.spend) &amp;lt; 2
order by cc, spend desc;
quit; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 03 Apr 2016 02:06:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260933#M50685</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-04-03T02:06:07Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find top two records of dataset using proc SQl</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260944#M50686</link>
      <description>&lt;P&gt;Thank you so much this helped me.&lt;img id="manhappy" class="emoticon emoticon-manhappy" src="https://communities.sas.com/i/smilies/16x16_man-happy.png" alt="Man Happy" title="Man Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 03 Apr 2016 12:01:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260944#M50686</guid>
      <dc:creator>LittlesasMaster</dc:creator>
      <dc:date>2016-04-03T12:01:36Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find top two records of dataset using proc SQl</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260945#M50687</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Thank you so much this helped me to understand the concept .&lt;/SPAN&gt;&lt;SPAN&gt;&lt;img id="manhappy" class="emoticon emoticon-manhappy" src="https://communities.sas.com/i/smilies/16x16_man-happy.png" alt="Man Happy" title="Man Happy" /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 03 Apr 2016 12:02:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260945#M50687</guid>
      <dc:creator>LittlesasMaster</dc:creator>
      <dc:date>2016-04-03T12:02:27Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find top two records of dataset using proc SQl</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260946#M50688</link>
      <description>&lt;P&gt;Thank you, this Rank was new for me&amp;nbsp;&lt;img id="manhappy" class="emoticon emoticon-manhappy" src="https://communities.sas.com/i/smilies/16x16_man-happy.png" alt="Man Happy" title="Man Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 03 Apr 2016 12:03:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-top-two-records-of-dataset-using-proc-SQl/m-p/260946#M50688</guid>
      <dc:creator>LittlesasMaster</dc:creator>
      <dc:date>2016-04-03T12:03:32Z</dc:date>
    </item>
  </channel>
</rss>

