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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.