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

_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

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

kstolzmann
Obsidian | Level 7

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  

Astounding
PROC Star

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.

kstolzmann
Obsidian | Level 7

This is helpful--thank you!

Kelly

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 5225 views
  • 3 likes
  • 3 in conversation