When I was analyzing table empT using SQL, the table is as following:
I want to find the minimum salary of managers, so I wrote the code as following:
proc sql;
select min(SAL) as min
from cert.emptt
where job='MANAGER';
quit;
I got my answer 2450, but I also want to know the manager's name, thus I wrote code as following:
proc sql;
select min(SAL) as min, ename
from cert.emptt
where job='MANAGER';
quit;
the I got the results:
It shows all manager's name with minimum salary, can you please let me know how can I get the answer of minimum salary with manager's name? Thanks so much
Use a HAVING clause:
proc sql;
select min(SAL) as min, ename
from cert.emptt
where job='MANAGER'
having calculated min = sal
;
quit;
Use a HAVING clause:
proc sql;
select min(SAL) as min, ename
from cert.emptt
where job='MANAGER'
having calculated min = sal
;
quit;
Thanks so much Sir, I got the right answer, but could you please explain why should I use having clause here? I am still confused
Maxim 3: Read the Log.
With your original code, you'll find a NOTE about remerging, so we need to prevent that.
To subset based on the result of a SQL summary function, you need HAVING, you cannot use it in a WHERE.
You can also do
proc sort
data=cert.emptt (where=(job = "MANAGER"))
out=want (obs=1)
;
by sal;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.