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

Using SAS 7.

 

New to proc sql. I don't quite understand what Proc sql: Group by and Having will give me.

 

Example: dataset have

 

Name Date Amount Spend
John Dec-17 15
John Oct-16 3
Peter Sep-16 70
Peter Feb-14 60

 

Will the below code output John as he has Amount_Spend<10? Or will only John record with Amount Spend=15 be output? Thank you

 

proc sql;
create table filtered As select * from have
group by name
having Amount_Spend> =10;
quit;
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

That's not a typical use of the HAVING clause, it's typically used to filter aggregate functions. In your case, it will work as the equivalent of a WHERE clause, so you should get everything except the first row in your results.

 

Here's an example that you should be able to run, where it picks anyone who has a height greater than the avg height, where avg height is calculated for each sex. 

 

proc sql;
create table want as
select *, mean(height) as avg_height
from sashelp.class
group by sex
having height>avg_height;
quit;

PS. I'm assuming you're using SAS EG 7? SAS 7 isn't a valid software version.

View solution in original post

5 REPLIES 5
LinusH
Tourmaline | Level 20
You need to use an aggregate function (sum) if you wish to filter on an aggregated value.
Data never sleeps
Reeza
Super User

That's not a typical use of the HAVING clause, it's typically used to filter aggregate functions. In your case, it will work as the equivalent of a WHERE clause, so you should get everything except the first row in your results.

 

Here's an example that you should be able to run, where it picks anyone who has a height greater than the avg height, where avg height is calculated for each sex. 

 

proc sql;
create table want as
select *, mean(height) as avg_height
from sashelp.class
group by sex
having height>avg_height;
quit;

PS. I'm assuming you're using SAS EG 7? SAS 7 isn't a valid software version.

LinusH
Tourmaline | Level 20
Just some SAS trivia: I think that there were a SAS7, but it was released to a very limited list of US customers who couldn't wait for V8.
Data never sleeps
jffeudo86
Quartz | Level 8
Having is used to filter grouped/aggregated numbers. In your example, amount_spend was not aggregated, so it will cause an error.

proc sql;
create table filtered As select name,sum(amount_spend) as Spent from have
group by name
having sum(Amount_Spend)> =10;
quit;

This code will have John->18 and Peter->130.

However:
proc sql;
create table filtered As select name,sum(amount_spend) as Spent from have
where amount_spend>=10
group by name
having sum(Amount_Spend)> =10;
quit;

will give you John->15 and Peter->130. John's amount_spend = 3 was filtered out by the where clause.
Reeza
Super User
It probably isn't super useful to respond to 3+ year old threads 😉

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 26148 views
  • 0 likes
  • 4 in conversation