Hi there,
I want to create certain number of macro variables within a macro. Those macro variables are used as input variables in a macro in a unique parameter. For example:
%macro example (vars);
data t;
input mylib.mydata (keep = &vars);
run;
%mend;
%example (vars= a b c);
Since the number of variables I pass into the macro each time can be different, I want to assign separate values to each one (e.g. a, b, and c) to perform certain calculations. I create a temporary dataset z in the macro as follow:
Var value
a 20
b 17
c 30
I’m not sure how to create three (or #) macro variables from the previous output. I’m trying something like shown below, but it is not working:
data _null_;
set z;
%do i=1 to &numb; /*previously determined in the macro (in this case 3)*/
%if _n_ = i %then call symputx ( “value&i” , value);
%end;
run;
Could you please advice?
Thanks a lot,
AG.
Could you please specify the expected result?
Do you mean that you want to create a macrovariable "a" that has the value 20, etc. ?
In this case, no need to have a do loop:
data _null_;
set z;
call symputx (var, value);
/*or*/
call symputx ("value"||left(_n_),value);
run;
Log:
87 %put &a. &b. &c.;
20 17 30
88 %put &value1. &value2. &value3.;
20 17 30
Could you please specify the expected result?
Do you mean that you want to create a macrovariable "a" that has the value 20, etc. ?
In this case, no need to have a do loop:
data _null_;
set z;
call symputx (var, value);
/*or*/
call symputx ("value"||left(_n_),value);
run;
Log:
87 %put &a. &b. &c.;
20 17 30
88 %put &value1. &value2. &value3.;
20 17 30
That's exactly what I wanted. Assign values to each one of the variables.
Thank you!
Hi,
You don't need a macro loop to create multiple macro variables with CALL SYMPUTX. Since the DATA step is a loop, who can use that, e.g.:
data lookup ;
input var $1. value ;
cards ;
a 20
b 17
c 30
;
data _null_ ;
set lookup ;
call symputx(cats("Value",_n_),value) ;
run ;
%put &=Value1 &=Value2 &=Value3 ;
Will create the three macro variable value1-value3:
400 %put &=Value1 &=Value2 &=Value3 ; VALUE1=20 VALUE2=17 VALUE3=30
And there are other ways to build a similar "array" of macro variables.
That said, I think your idea of creating a dataset Z with name-value pairs was a good one. Instead of reading that dataset to create macro variables and using those macro variables to pass values to your macro, it might be cleaner to simply pass name of the dataset Z to your macro. That often allows you to avoid the mess of going from values stored in a SAS dataset, to values stored as text in macro variables, and then back to values stored in SAS datasets.
Perfect, thank you very much.
I am not understanding your question at all. Your example macro has ONE parameter. It accepts a list of VARIABLE names. How do macro variables come into the problem? Are they the input or the output? Where?
Are you saying you want to create macro variables that have the same names as the actual variables? Just use CALL SYMPUTX(). You can force the macro variables into the global symbol table using the third parameter.
If the variables are all of the same type you can use an array.
array x &vars;
do i=1 to dim(x);
call symputx(vname(x[i]),x[i],'g');
end;
If not then you need to make a macro loop:
%do i=1 %to %sysfunc(countw(&vars),%str( ));
call symputx("%scan(&var,&i,%str( ))",%scan(&var,&i,%str( )),'g');
%end;
Sorry if I was not clear enough for you. I already got some replies with the solution. Very much appreciated your input.
please try
data z;
input Var$ value;
cards;
a 20
b 17
c 30
;
data _null_;
set z;
do i=1 to 3; /*previously determined in the macro (in this case 3)*/
if _n_ = i then call symputx (var , value);
end;
run;
%put &a &b &c;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.