Find 3RD highest salary for the below scnario
ID name Salary
1 AM 1000
2 AD 40000
3 AE 50000
5 AF 50000
6 AK 15000
7 AS 2500
data have;
input ID name $ Salary;
datalines;
1 AM 1000
2 AD 40000
3 AE 50000
5 AF 50000
6 AK 15000
7 AZ 15000
8 AS 2500
;
proc sql;
select a.id,a.name,a.Salary,count(distinct b.salary) as n
from have as a,have as b
where a.salary<b.salary
group by a.id,a.name,a.Salary
having calculated n=2
order by 1
;
quit;
What if 2 obs have the third highest salary? Do you want both obs?
Please post your desired result given this input data.
data have;
input ID name $ Salary;
datalines;
1 AM 1000
2 AD 40000
3 AE 50000
5 AF 50000
6 AK 15000
7 AZ 15000
8 AS 2500
;
50000 is not the third highest salary?
Anyways, use Proc Rank and apply the ties option to your liking.
data have;
input ID name $ Salary;
datalines;
1 AM 1000
2 AD 40000
3 AE 50000
5 AF 50000
6 AK 15000
7 AZ 15000
8 AS 2500
;
proc rank data = have out = rank descending ties = dense;
var Salary;
ranks r;
run;
Why does it have to be SQL?
data have;
input ID name $ Salary;
datalines;
1 AM 1000
2 AD 40000
3 AE 50000
5 AF 50000
6 AK 15000
7 AZ 15000
8 AS 2500
;
proc sql;
select a.id,a.name,a.Salary,count(distinct b.salary) as n
from have as a,have as b
where a.salary<b.salary
group by a.id,a.name,a.Salary
having calculated n=2
order by 1
;
quit;
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.