BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Naveen1
Calcite | Level 5

Question : I have a dataset with department names, employee names, salary. I have to find out the 5th largest salary of employee along with its department by using Proc SQL (I know the procedure by using Data step but I want to know by using Proc SQL). Please help.

 

Dept_Name Emp_name Salary
Economics Naveen 1070
statistics Praveen 1157
Biology Manju 1286
marketing Chethan 1396
Economics vinay 1110
statistics sanjay 1318
Biology harshi 1376
marketing manu V 1179
Biology manu S 1002
statistics manu P 1442
1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

Hi,

 

This is another way:

 

proc sql outobs=5;
create table want as
select * from have
order by salary desc;
select * from want
having salary=min(salary);
quit;

View solution in original post

12 REPLIES 12
PaigeMiller
Diamond | Level 26

Is this a homework problem?

 

I would advise to do this in a data step or PROC RANK. It's much simpler.

--
Paige Miller
Naveen1
Calcite | Level 5

I want to know in Proc SQl.

I know the procedure in Data step by using rank and (_n_).

Loko
Barite | Level 11

Post yout trial here and you will be helped for sure. Have you tried solve it by yourself?

Naveen1
Calcite | Level 5

Iam not that much good in Proc SQL. When I searched online I could't get exact solution for 5th largest. I got solution for 2nd largest, 3rd largest. So please help me how to get 5th Largest. Thanks in advance.

PaigeMiller
Diamond | Level 26

Can you provide a link to the solution for 3rd largest? Why wouldn't that generalize to the 5th largest? Show us your code!

 

And while it is admirable to try to increase your SQL skills, this really isn't something that you should attempt to do in SQL, since SQL is simply a very poor choice in SAS to do this calculation.

--
Paige Miller
Naveen1
Calcite | Level 5

The above problem is interviewed question. So Iam seeking help in this portal. Thanks.

PaigeMiller
Diamond | Level 26

@Naveen1 wrote:

The above problem is interviewed question. So Iam seeking help in this portal. Thanks.


The answer to the interview question is:

 

"It would be easier to do this in a DATA step or using PROC RANK. Doing this in PROC SQL is a very poor choice."

--
Paige Miller
gamotte
Rhodochrosite | Level 12

1/ Sort by Salary (with a SELECT ... ORDER BY if you have to use SQL only)

2/ Use the monotonic function to get the fifth record.

Naveen1
Calcite | Level 5

Can I get elabprated syntax, as I informed earlier I dont have much knowledge on  Proc SQL.

LinusH
Tourmaline | Level 20

You have still not motivated why you want/need to do it in SQL.

If it's for learning purposes, don't do it, since given the answer already given, there are better ways to do this. Don't learn a less efficient way.

If you want to learn SQL, there must be tons of other exercises to go about.

Data never sleeps
Ksharp
Super User
Why you want to do this. it is really clumsy for SQL to get the largest n obs.
The following return the 5th largest weight for each sex.



proc sql;
select sex,name,weight,(select count(*) as n  from (select distinct sex,name,weight from sashelp.class) as b
          where b.sex=a.sex and b.weight<=a.weight) as n
 from sashelp.class as a
   where calculated n=5;
quit;

stat_sas
Ammonite | Level 13

Hi,

 

This is another way:

 

proc sql outobs=5;
create table want as
select * from have
order by salary desc;
select * from want
having salary=min(salary);
quit;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 2327 views
  • 2 likes
  • 7 in conversation