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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.