- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
_sym
Hello,
I am trying to nest call symput inside a simple macro. I've tried something like this but can't get it to work:
%macro mean_std (var);
data _null_;
set testmean;
call symputx ("&&var._Mean", &var._Mean);
run;
%put the mean of &var is &&var._Mean;
%mend mean_std;
%mean_std (CarePractice);
Any ideas? Thank you!
Kelly
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes, you have additional ampersands (&):
%macro mean_std (var);
data _null_;
set testmean;
call symputx("var_Mean", &var._Mean);
run;
%put the mean of &var is &var_Mean;
%mend mean_std;
%mean_std (CarePractice);
However I think you maybe unclear on the use of this. I don't see an awfull lot of value in trying to unreference the variable to create a new variable like that. If you are trying to create a new variable for the mean then:
%macro mean_std (var);
data want;
set sashelp.cars (obs=1);
attrib &Var._mean format=best.;
&var._mean=mean(&var.);
call symput('result',&var._mean);
run;
%put the mean of &var is &result.;
%mend mean_std;
%mean_std (CarePractice);
Maybe some test data and required output would help clarify.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes, you have additional ampersands (&):
%macro mean_std (var);
data _null_;
set testmean;
call symputx("var_Mean", &var._Mean);
run;
%put the mean of &var is &var_Mean;
%mend mean_std;
%mean_std (CarePractice);
However I think you maybe unclear on the use of this. I don't see an awfull lot of value in trying to unreference the variable to create a new variable like that. If you are trying to create a new variable for the mean then:
%macro mean_std (var);
data want;
set sashelp.cars (obs=1);
attrib &Var._mean format=best.;
&var._mean=mean(&var.);
call symput('result',&var._mean);
run;
%put the mean of &var is &result.;
%mend mean_std;
%mean_std (CarePractice);
Maybe some test data and required output would help clarify.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you, this works! My CarePractice_Mean value is from an aggregated dataset so I want to use this value on my original data (in calculations). I suppose I could have re-merged the aggregated dataset back to my original file (and I'm sure there are other more advanced ways of approaching this problem (using sql, etc.)). Really, I was just trying to avoid typing in "36.7665" every time I needed to use the overall mean and it was bothering me that the code wasn't working (in case I really needed to use this type of call symput inside a macro in the future). Thanks again for your help!
Kelly
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
In case it would help, here is a fairly standard way to combine a single observation data set with a multiple observation data set:
data want;
if _n_=1 then set testmean;
set original_data;
run;
It is possible that you would eliminate the macro language entirely.
Good luck.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This is helpful--thank you!
Kelly