SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
hmlong25
Obsidian | Level 7
proc sql;
select avg(PopEstimate1), "Average Estimated Population"
from sq.statepopulation;
quit;
 
 
 
proc sql;
select Name, PopEstimate1
from sq.statepopulation
where PopEstimate1 > (select avg(PopEstimate1),"Average Estimated Population"
from sq.statepopulation);
quit;
 
 
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@hmlong25 wrote:
Thank you! Is it easier to use the Having clause when the query requires remerging summary stats back with the original instead of using a subquery as a general rule?

That is a matter personal preferance.

I find it easier and clearer.

 

But if you have to use other SQL dialects you might not want to use it since they generally not support it so you might want to use a method that works the same in all of the SQL dialects you have to use commonly.

View solution in original post

5 REPLIES 5
A_Kh
Barite | Level 11

In your first code comma (,) separates the variable and it's label which is incorrect.  Comma separates only variables. So remove the comma and see if this works. 

proc sql;
select avg(PopEstimate1)  "Average Estimated Population"
from sq.statepopulation;
quit;
 
hmlong25
Obsidian | Level 7
The comma put the Label next to the average amount instead of above it. I should not have the comma, I agree. I think the Label= option is a better one also
Tom
Super User Tom
Super User

What are you trying to do?

 

Your first query returns two variables (what you called columns), neither of which has been given a NAME.  Let's change it to use SASHELP.CLASS so we can experiment easily.

proc sql;
select avg(age), "Average Estimated Age"
from sashelp.class;
quit;

Result

Tom_0-1739214929810.png

If you want to give that variable a LABEL then remove the comma.

proc sql;
select avg(age) "Average Estimated Age"
from sashelp.class;
quit;

Result

Tom_1-1739215122715.png

You might also want to give the variable a NAME.

proc sql;
select avg(age) as avg_age "Average Estimated Age"
from sashelp.class;
quit;

To be clearer that the quoted string is just the LABEL for the variable add the LABEL= keyword.

proc sql;
select avg(age) as avg_age label="Average Estimated Age"
from sashelp.class;
quit;

For your second query you need to return only ONE value in the subquery to be able to use it with the > comparison operator.  So again you need to remove the comma.  You could leave the label, but why? There is no way for that label to be printed or saved anywhere with the way that query is constructed. 

proc sql;
select Name, age
  from sashelp.class
  where age > (select avg(age) from sashelp.class)
;
quit;

You can avoid the subquery (at least in PROC SQL) if you use the HAVING clause instead of the WHERE clause.

proc sql;
select Name, age
  from sashelp.class
  having age > avg(age)
;
quit;

Because as the SAS log states PROC SQL will remerge the aggregate function results for you.

504  proc sql;
505  select Name, age
506    from sashelp.class
507    having age > avg(age)
508  ;
NOTE: The query requires remerging summary statistics back with the original
      data.
509  quit;

 

hmlong25
Obsidian | Level 7
Thank you! Is it easier to use the Having clause when the query requires remerging summary stats back with the original instead of using a subquery as a general rule?
Tom
Super User Tom
Super User

@hmlong25 wrote:
Thank you! Is it easier to use the Having clause when the query requires remerging summary stats back with the original instead of using a subquery as a general rule?

That is a matter personal preferance.

I find it easier and clearer.

 

But if you have to use other SQL dialects you might not want to use it since they generally not support it so you might want to use a method that works the same in all of the SQL dialects you have to use commonly.

sas-innovate-white.png

Join us for our biggest event of the year!

Four days of inspiring keynotes, product reveals, hands-on learning opportunities, deep-dive demos, and peer-led breakouts. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 496 views
  • 2 likes
  • 3 in conversation