BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
vishalrajpoot3
Obsidian | Level 7

Hello SAS Community,

I am facing problem while I am passing the macro variable in mean or any other aggregation function in SAS SQL. It is providing mean of first variable only but I want mean of all the variable and group by with names. Below is the code for your reference:

 

data new; set sashelp.class;
if name = 'Alfred' then name = 'Alice';
If name = 'Janet' then name = 'Judy';
if name = 'Robert' then name = 'Ronald';
run;


data a;
set sashelp.class(keep=Height Weight);
run;


proc contents data=a out=b(keep=name);
run;

 

proc sql;
select Name into: Name_4 separated by ','  from b;
quit

 

proc sql;
create table new_1 as select Name, mean(&Name_4.) as M_&Name_4.
from new group by name;
run;

A quick help is much appreciated. 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Note that PROC SQL is NOT the right tool for generating the mean of multiple variables.  Use PROC MEANS (aka PROC SUMMARY).

 

But to fix your current code you need to do two things.

1) Figure out what SQL code you need to generate.

2) Change your macro variable to contain that code.

 

data new ;
  set sashelp.class;
  if name = 'Alfred' then name = 'Alice';
  if name = 'Janet' then name = 'Judy';
  if name = 'Robert' then name = 'Ronald';
run;

proc contents noprint data=new(keep=height weight) out=names(keep=name);
run;


proc sql noprint;
select catx(' ','mean(',Name,') as',cats('m_',name))
  into :sql_code separated by ','  
  from names
;
%put &=sql_code ;
create table new_1 as
  select Name, &sql_code
  from new 
  group by name
;
quit;

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

I don't understand this. What is your desired result given this sample data? Please show us.

Kurt_Bremser
Super User

If you want to calculate means (or other statistics) for multiple variables, PROC SUMMARY is the tool of choice.

If you have a macro variable containing your variable names (separated by blanks), it's just

proc summary data=new nway;
class name;
var &names.;
output out=want mean()=;
run;
vishalrajpoot3
Obsidian | Level 7

There are multiple variables I have and multiple aggregation needed. After that I need to group by on names.

Quentin
Super User

Your current code results in a macro variable &Name_4 with the value: Height,Weight.  

 

When you code:

proc sql;
create table new_1 as select Name, mean(&Name_4.) as M_&Name_4.
from new group by name;
quit ;

After the macro substitution your code is:

proc sql;
create table new_1 as select Name, mean(Height,Weight) as M_Height,Weight
from new group by name;
quit ;

That code will not calculate the mean of Height.  It calculates the mean of height and weight, for each record.  Note you should have a warning in your log, because the the expression does not have a summary function:

WARNING: A GROUP BY clause has been transformed into an ORDER BY clause because neither the SELECT
         clause nor the optional HAVING clause of the associated table-expression referenced a
         summary function.

 If you would approach this with the macro language, you will need a macro to generate SQL code like:

proc sql;
create table new_1 as select Name
, mean(Height) as M_Height
, mean(Weight) as M_Weight
from new group by name;
quit ;

This is a straight forward macro exercise, because the macro language is good at processing lists.  So you can take a list of variable names, and use it to generate a list of SQL statements.  But as Kurt mentioned, PROC SUMMARY will cost you less code to write.

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Tom
Super User Tom
Super User

Note that PROC SQL is NOT the right tool for generating the mean of multiple variables.  Use PROC MEANS (aka PROC SUMMARY).

 

But to fix your current code you need to do two things.

1) Figure out what SQL code you need to generate.

2) Change your macro variable to contain that code.

 

data new ;
  set sashelp.class;
  if name = 'Alfred' then name = 'Alice';
  if name = 'Janet' then name = 'Judy';
  if name = 'Robert' then name = 'Ronald';
run;

proc contents noprint data=new(keep=height weight) out=names(keep=name);
run;


proc sql noprint;
select catx(' ','mean(',Name,') as',cats('m_',name))
  into :sql_code separated by ','  
  from names
;
%put &=sql_code ;
create table new_1 as
  select Name, &sql_code
  from new 
  group by name
;
quit;

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