BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10

Hi 

How to find 4th max value in proc sql 

9 REPLIES 9
PaigeMiller
Diamond | Level 26

Use PROC SORT and a DATA step, or PROC RANK.

 

PROC SQL is a particularly poor tool for this task.

--
Paige Miller
SteveDenham
Jade | Level 19

PROC UNIVARIATE will default to outputting the top 5 ranking values and the bottom 5 ranking values, so you might consider it as well.

 

SteveDenham

BrahmanandaRao
Lapis Lazuli | Level 10
Hi PaigeMiller
in proc sql this cannot solve the problem
please tell me
PaigeMiller
Diamond | Level 26

@BrahmanandaRao wrote:
Hi PaigeMiller
in proc sql this cannot solve the problem
please tell me

I already told you, PROC SQL is a very poor choice for this problem. The solution is simple using PROC SORT or PROC RANK.

--
Paige Miller
Ksharp
Super User

SQL is not right tool for this.

 

data have;
 set sashelp.class;
run;
proc sql;
create table want as
select sex,weight,
 (select count(distinct weight) from have where sex=a.sex and weight>=a.weight) as n
 from have as a
  where calculated n=4;
quit;
PaigeMiller
Diamond | Level 26

On of the reasons you would want to use PROC RANK and not SQL is that PROC RANK gives you control over how ties are handled, which could be important for real data (although it doesn't matter with SASHELP.CLASS).

--
Paige Miller
BrahmanandaRao
Lapis Lazuli | Level 10

Hi K Sharp

Is it correct method to find nth max value

 

data _4th;
input id salary;
datalines;
1 2000
2 6000
3 3000
4 9000
5 4500
6 8000
;
run;


proc sql;
create table nmax  as
select id, salary from _4th
order by salary desc
;
select id ,salary from nmax
where monotonic() =4;
quit;
Ksharp
Super User
No. I would not trust function "monotonic()" .
It would get the unpredicted result according to sas documentation .
Pick right tool -> data step .

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 9 replies
  • 1509 views
  • 2 likes
  • 5 in conversation