How to evaluate a macro variable after variable was created in proc summary? SEE BELOW
SAS CODE
data summaryOut; set summaryOut;
file print;
put meanY; run;
meanY is in summaryOut dataset. I want a function to evaluate meanY (=23083).
data _null_; set summaryOut;
%let meanY=%SYSFUNC(meanY);
%put &meanY;
*meanY=23083.164953;
run; quit;
87 data _null_; set summaryOut;
88 %let meanY=%EVAVL(meanY);
ERROR: Expected open parenthesis after macro function name not found.
89 %put &meanY;
SYMBOLGEN: Macro variable MEANY resolves to
91 run;
So far, you don't have a macro variable holding any meaningful information. PROC SUMMARY does not create macro variables.
Your first DATA step shows the value of a DATA step variable MEANY. If you want to get that value into a macro variable, you could use:
data _null_;
set summaryOut;
call symputx('meany', meany);
run;
%put &meanY;
That gives you a macro variable holding the contents of the DATA step variable.
So far, you don't have a macro variable holding any meaningful information. PROC SUMMARY does not create macro variables.
Your first DATA step shows the value of a DATA step variable MEANY. If you want to get that value into a macro variable, you could use:
data _null_;
set summaryOut;
call symputx('meany', meany);
run;
%put &meanY;
That gives you a macro variable holding the contents of the DATA step variable.
How to evaluate a macro variable after variable was created in proc summary?
inspired by
https://goo.gl/hPzyHa
https://communities.sas.com/t5/General-SAS-Programming/How-to-evaluate-a-macro-variable-after-variable-was-created-in/m-p/342401
HAVE
====
Up to 40 obs WORK.CLASS total obs=5
Obs Y
1 14
2 13
3 13
4 14
5 14
WANT ( Something like this )
============================
data _null_;
set summaryout
meany = &meany;
put " then mean of y is " meany;
run;quit;
FULL SOLUTION
=============
data class;
set sashelp.class(keep=age obs=5 rename=age=y);
run;quit;
proc summary data=class;
var y;
output out=summaryout(keep=meany) mean=meanY;
run;quit;
Up to 40 obs from summaryout total obs=1
Obs MEANY
1 13.6
%symdel mac_meany;
data _null_;
* create macro variable from summaryout;
if _n_=0 then do;
%let rc=%sysfunc(dosubl('
data _null_;
set summaryout;
call symputx("mac_meany",put(meany,best.),"G");
run;quit;
'));
%put &=mac_meany;
end;
meany = &mac_meany;
put " The mean of y is " meany;
run;quit;
The mean of y is 13.6
@MaryA_Marion wrote:
How to evaluate a macro variable after variable was created in proc summary? SEE BELOW
SAS CODE
data summaryOut; set summaryOut;
file print;
put meanY; run;
meanY is in summaryOut dataset. I want a function to evaluate meanY (=23083).
data _null_; set summaryOut;
%let meanY=%SYSFUNC(meanY);
%put &meanY;
*meanY=23083.164953;
run; quit;
87 data _null_; set summaryOut;
88 %let meanY=%EVAVL(meanY);
ERROR: Expected open parenthesis after macro function name not found.
89 %put &meanY;
SYMBOLGEN: Macro variable MEANY resolves to
91 run;
Unfortunately your code doesn't make sense.
It looks like you're trying to create a macro variable for a mean and then use it in another step. Rather than a macro, a merge works as well, depending on usage of course.
*Add average value to a dataset;
*Solution 1 - PROC MEANS + Data step;
proc means data=sashelp.class noprint;
output out=avg_values mean(height)=avg_height;
run;
data class;
set sashelp.class;
if _n_=1 then
set avg_values;
run;
proc print data=class;
run;
*Solution 2 - PROC SQL - note the warning in the log;
PROC SQL;
Create table want as
select *, mean(height) as avg_height
from sashelp.class;
quit;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.