call symput inside a macro?

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

call symput inside a macro?

_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
Solution
‎07-29-2014 08:32 AM
Super User
Super User
Posts: 7,727

Re: call symput inside a macro?

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.

View solution in original post


All Replies
Solution
‎07-29-2014 08:32 AM
Super User
Super User
Posts: 7,727

Re: call symput inside a macro?

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.

Contributor
Posts: 23

Re: call symput inside a macro?

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  

Super User
Posts: 5,372

Re: call symput inside a macro?

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.

Contributor
Posts: 23

Re: call symput inside a macro?

This is helpful--thank you!

Kelly

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 410 views
  • 3 likes
  • 3 in conversation