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

Hello

In this code using Re-merging technique.

I wonder if need to use WHERE clause or HAVING clause in order to choose only students with Math score below overall mean on Math.

Please note tht I haven't run this code and asked a theoretical question.

PROC SQL;
Create table Wanted  AS
select Gender,
	mean(Math) as average_Math
from ttt
WHERE  Math<average_Math
;
QUIT;
 
1 ACCEPTED SOLUTION

Accepted Solutions
sbxkoenk
SAS Super FREQ

Hello,

 

This does NOT work :

PROC SQL;
Create table Wanted  AS
select   Name , Sex
	   , mean(Height) as average_Height
from sashelp.class
where Height < calculated average_Height
;
QUIT;

And this works well :

PROC SQL;
Create table Wanted  AS
select   Name , Sex
	   , mean(Height) as average_Height
from sashelp.class
having Height < average_Height
;
QUIT;

 

Koen

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

If you make up a simple data set, you can find the answer yourself.

--
Paige Miller
Reeza
Super User
Did you test your theory?
What happened?
sbxkoenk
SAS Super FREQ

Hello,

 

This does NOT work :

PROC SQL;
Create table Wanted  AS
select   Name , Sex
	   , mean(Height) as average_Height
from sashelp.class
where Height < calculated average_Height
;
QUIT;

And this works well :

PROC SQL;
Create table Wanted  AS
select   Name , Sex
	   , mean(Height) as average_Height
from sashelp.class
having Height < average_Height
;
QUIT;

 

Koen

PaigeMiller
Diamond | Level 26

@Ronein wrote:

Hello

In this code using Re-merging technique.

I wonder if need to use WHERE clause or HAVING clause in order to choose only students with Math score below overall mean on Math.

Please note tht I haven't run this code and asked a theoretical question.

PROC SQL;
Create table Wanted  AS
select Gender,
	mean(Math) as average_Math
from ttt
WHERE  Math<average_Math
;
QUIT;
 

Gosh, I also hate the idea of using PROC SQL for EVERYTHING, especially statistical things (although certainly PROC SQL can do this). There are plenty of other ways to answer this question which would be helpful to learn, and would show nice growth if people branched out from SQL into other SAS PROCs. This also has a lot more power in the long run and for real-world problems. I have this problem in my group (so this isn't directed at anyone in particular)

 

Example:

 

proc stdize data=sashelp.class method=mean sprefix=delta_ out=want(where=(delta_height<0));
    var height;
run;
--
Paige Miller
Ronein
Meteorite | Level 14
May you please explain why dinyiu hate using re-meege via proc sql?
As I understand this ability exists only in sas and cannot be done in Microsoft sql language.
Reeza
Super User
It's not appropriate for the use case you've shown but it has it's purposes.
PaigeMiller
Diamond | Level 26

@Ronein wrote:
May you please explain why dinyiu hate using re-meege via proc sql?
As I understand this ability exists only in sas and cannot be done in Microsoft sql language.

I already explained why I feel the way I do. 

--
Paige Miller
novinosrin
Tourmaline | Level 20

Hi @Ronein , Your question per se is good, albeit warrants a tutorial approach than a short explanation. 

 

Short answer:

HAVING clause in general applies for conditional vertical processing for columns as a whole or for BY groups using a summary function, whereas the WHERE clause strictly is not designed and is incompatible for vertical column summary. Therefore WHERE isn't for summary and then filter, rather you could deem this a mere row filter.

 

To begin with, some teaser for you to figure why both HAVING and WHERE for a calculated column works below-


/*Using calculated & WHERE */
PROC SQL;
Create table Wanted  AS
select   *,mean(100, weight) as w
from sashelp.class
where  calculated w < 100
;
QUIT;

/*Using HAVING*/
PROC SQL;
Create table Wanted  AS
select   *,mean(100, weight) as w
from sashelp.class
having   w < 100
;
QUIT;

Tips- 1. Think about how a SQL processor works i.e. read from disk, buffer, memory(cache), processing 2. Timing - what executes when(before or after) 3. Why there's no REMERGE in the above example?

 

I will try and come back with a detailed explanation if my boss gives me some time off. 🙂 lol   

 

 

 

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
  • 8 replies
  • 527 views
  • 6 likes
  • 5 in conversation