BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Anandkvn
Lapis Lazuli | Level 10

Hi Guys,

 

using Proc sql  how to find top Nth Highest salaries for each department wise even if same salary respective departments 

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @Anandkvn 

 

PROC RANK would be a great tool to achieve this:

data have;
	input dpt $ employeeID salary;
	datalines;
A 1 100
A 2 110
A 3 90
B 1 120
B 2 70
B 3 60
B 4 130
;
run;

proc rank data=have out=want (where=(top_salary<=2)) descending; /* instead of "2", put the top "n" that you want*/
	by dpt;
	var salary;
	ranks top_salary;
run;

View solution in original post

5 REPLIES 5
ed_sas_member
Meteorite | Level 14

Hi @Anandkvn 

 

PROC RANK would be a great tool to achieve this:

data have;
	input dpt $ employeeID salary;
	datalines;
A 1 100
A 2 110
A 3 90
B 1 120
B 2 70
B 3 60
B 4 130
;
run;

proc rank data=have out=want (where=(top_salary<=2)) descending; /* instead of "2", put the top "n" that you want*/
	by dpt;
	var salary;
	ranks top_salary;
run;
ed_sas_member
Meteorite | Level 14

As an alternative, and especially if you have a huge dataset, a HASH can be useful:

data have;
	input dpt $ employeeID salary;
	datalines;
A 1 100
A 2 110
A 3 90
B 1 120
B 2 70
B 3 60
B 4 130
;
run;

data want;
	declare hash h(multidata: 'y', ordered: 'd');
	h.definekey('salary');
	h.definedata('dpt', 'employeeID', 'salary');
	h.definedone();
	declare hiter C('h');

	do until(last.dpt);
		set have;
		by dpt;
		h.add();
	end;
	
	C.first();
	do i=1 to 2; /*instead of 2, put the top'n' that you want*/
		output;
		C.next();
	end;
	
	drop i;
run;
Anandkvn
Lapis Lazuli | Level 10

I want proc sql query not proc rank and hash table

ed_sas_member
Meteorite | Level 14

"Saying thank you is more than good manners. it is good spirituality"

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 461 views
  • 0 likes
  • 3 in conversation