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

i want to find 2nd highest salary of each employee.

 

data sal;
input id name$ salary dt date11.;
format dt date9.;
cards;
101 nick 45000 01jan2019
101 nick 50000 01feb2019
101 nick 52000 01mar2019
101 nick 53000 01apr2019
102 mark 55000 01jan2019
102 mark 56000 01feb2019
102 mark 57000 01mar2019
102 maek 58000 01apr2019
;run;

 

i have done using sql

 

proc sql;
select max(T.salary) as sal,T.id from sal
T inner join (select max(salary) as sal,
id from sal group by id) TT on T.salary<>TT.sal and
T.id=TT.id group by T.id;
run;

 

Is there any more simple method in SQL . 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ghosh
Barite | Level 11

 

Capture.JPG

proc rank data=sal descending out=ranking ties=dense;
   by id;
	var salary;
    ranks salrank;
run;

proc print n;
  where salrank=2;
run;

View solution in original post

10 REPLIES 10
Shmuel
Garnet | Level 18

Why not use base sas / data step?

data sal;
input id name$ salary dt date11.;
format dt date9.;
cards;
101 nick 45000 01jan2019
101 nick 50000 01feb2019
101 nick 52000 01mar2019
101 nick 53000 01apr2019
102 mark 55000 01jan2019
102 mark 56000 01feb2019
102 mark 57000 01mar2019
102 maek 58000 01apr2019
;run;

proc sort data=sal; by id descending salary; run;

data want;
 set sal;
      by id;
           retain flag;
           if first.id then flag=1;
           else if flag=1 then do;
               output; flag=0;
          end;
run;    
yabwon
Onyx | Level 15

May I offer small extension to your code? It gives more flexibility.

 

all the best

Bart

 

proc sort data=sal; 
  by id descending salary; 
run;

data want;
  set sal;
  by id;

  retain flag;
  if first.id then flag=1;
              else flag = flag + 1;

  if flag=2 then /* 2 - second, 3 = third, ...*/
  do;
    output; 
    flag=.;
  end;
run;    
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Ksharp
Super User

proc univariate data=sal nextrval=2;
by id;
var salary;
run;
PeterClemmensen
Tourmaline | Level 20

You can add something like the below to @Ksharp s code to get a SAS data set with the desired data

 

ods output ExtremeValues=ExtremeValues(where=(HighOrder=3) keep=id High HighOrder);
ghosh
Barite | Level 11

 

Capture.JPG

proc rank data=sal descending out=ranking ties=dense;
   by id;
	var salary;
    ranks salrank;
run;

proc print n;
  where salrank=2;
run;
KachiM
Rhodochrosite | Level 12

A simple solution.

 

proc sort data=sal; by id descending salary; run;

data need;
   do i = 1 by 1 until(last.id);
      set sal;
      by id;
      if i = 2 then output;
   end;
run;
Ksharp
Super User

What if there are two same max salary ? Like:

 

id salary

1   10

1   10

1     8

1     6

2     10

...........

KachiM
Rhodochrosite | Level 12

I didn't see the ties requirement of OP. There are many answers for that and let OP choose.

ghosh
Barite | Level 11

To ensure all ties have the same rank I used the Proc rank option ties=dense.  In your example data salary 10 is tied at #1 and the answer for the OP (Rank 2) will be salary of 8 for ID 1

 

 

Capture.JPG

 

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 10 replies
  • 8021 views
  • 10 likes
  • 8 in conversation