BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi all,
I have a question regarding proc sql sintax.
I need to find top 5 rows in a query which contains group by.
In particolar I need top 5 for each aggegation groups and not 5 rows at all.
In Oracle sintax I think I could use "Partition By" over a given rank in order to apply this wish.
Could you please suggest me a workaround to use in proc sql or a specific function if existing?

I really thank you a lot in advance for your support!

KR

Daniele.
1 REPLY 1
Patrick
Opal | Level 21
Hi Daniele

As found under http://groups.google.com/group/comp.soft-sys.sas/msg/3daf525de6e48d59

HTH
Patrick

data have;
input YEAR ASSETS;
datalines;
1998 10
1998 20
1998 30
1998 40
1998 50
1998 60
1998 70
1998 80
1997 500
1997 600
1997 700
1997 800
1997 100
1997 200
1997 300
1997 400
;

dm 'clear log; clear output';
Title 'Top 5' ;
proc sql ;
select a.*
from have as a
left join have as b on (a.year=b.year and a.assets le b.assets)
group by a.year, a.assets
having count(*) le 5
order by a.year, a.assets desc ;
quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 857 views
  • 0 likes
  • 2 in conversation