SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
harpaljosan
Calcite | Level 5

How to find the Nth largest salary from a sas dataset using Proc SQL?

 

EMPNO Salary empcode

111 4000 A
112 6000 A
114 2000 A
115 8000 A
223 2000 B
226 1000 B
228 3000 B
300 500 C
333 700 C
345 300 C
356 200 C
320 700 C

i want to find out the 3rd largest salary group by empcode using Proc Sql.

7 REPLIES 7
Ksharp
Super User

I really suggest you to use data step , NOT Sql.

data have;
input EMPNO Salary empcode $;
cards;
111 4000 A
112 6000 A
114 2000 A
115 8000 A
223 2000 B
226 1000 B
228 3000 B
300 500 C
333 700 C
345 300 C
356 200 C
320 700 C
;
run;
proc sql;
create table want as
 select *,
  (select count(*) from 
    (select distinct Salary,empcode from have) as b 
   where b.empcode=a.empcode and b.Salary gt a.Salary) as n
  from have as a
   where calculated n=2;
quit;
PaigeMiller
Diamond | Level 26

PROC RANK will do this as well.

 

I agree with the others, it is really pointless to insist on doing this in PROC SQL

--
Paige Miller
sas_guru
Calcite | Level 5

this program will not work if there are multiple salaries with the same value.

 

Kurt_Bremser
Super User

But the data step can easily be adapted to deal with groups:

data want;
set have;
by salary;
if first.salary then n + 1;
if n > number_you_want then stop;
if n = number_you_want;
run;
PaigeMiller
Diamond | Level 26

@sas_guru wrote:

this program will not work if there are multiple salaries with the same value.

 


As I said above, PROC RANK is the tool to use, it will find multiple salary values with the same values.

--
Paige Miller

sas-innovate-white.png

Our biggest data and AI event of the year.

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.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 7 replies
  • 12292 views
  • 4 likes
  • 5 in conversation