SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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