BookmarkSubscribeRSS Feed
Aexor
Lapis Lazuli | Level 10

Hi ,

I have below table. I need to find out employee names whose salary is greater than his manager 

 

emp_id  name   dept   sal   manager_id
1           A         HR     200          2
2          B         Fin     300            3
3         C         anaytics  400        4
4         D            QA       100        1

2 REPLIES 2
sbxkoenk
SAS Super FREQ
data have0;
LENGTH emp_id 8 name $ 1 dept $ 10 sal 8 manager_id 8;
input emp_id  name $  dept $  sal  manager_id;
datalines;
1       A      HR         200   2
2       B      Fin        300   3
3       C      analytics  400   4
4       D      QA         100   1
;
run;

PROC SQL noprint;
 create table have1 as
 select t1.* , t2.sal as sal_mgr
 from   work.have0                  as t1
      , work.have0(keep=emp_id sal) as t2
 where t1.manager_id = t2.emp_id
 order by t1.emp_id , t1.name
;
QUIT;

data want;
 set have1;
 where sal > sal_mgr;
run;
/* end of program */

Koen

mkeintz
PROC Star

A reasonable solution to this problem is a hash object:

 

data have0;
LENGTH emp_id 8 name $ 1 dept $ 10 sal 8 manager_id 8;
input emp_id  name $  dept $  sal  manager_id;
datalines;
1       A      HR         200   2
2       B      Fin        300   3
3       C      analytics  400   4
4       D      QA         100   1
;
run;


data want;
  set have0   have0 (obs=0 rename=(sal=_mgr_sal));
  if _n_=1 then do;    
    declare hash h (dataset:'have0 (keep=sal emp_id rename=(emp_id=manager_id sal=_mgr_sal)');
      h.definekey('manager_id');
      h.definedata('_mgr_sal');
      h.definedone();
  end;
  if h.find()=0 and sal>_mgr_sal;
run;

The reason for the second HAVE0 in the SET statement is just to prompt the SAS compiler to allocate a place in the program data vector for the variable _mgr_sal.  Mentioning it in the DECLARE statement is not sufficient.  But notice the second HAVE0 has "obs=0" so no extra data is generated.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 539 views
  • 2 likes
  • 3 in conversation