Hi folks,
I have some problem to call a series of macro variables from a table.
Say I have a table stored some values, namely:
Table1
x1 x2 x3
5 7 6
I want to use the call symputx function to call all the macro variables at the same time. Basically I am wrting a code like:
Data storedvalue;
Table1;
do i=1 to 3;
call symputx("macroi",xi);
end;
run;
However, it's not working since i is not evaluated. Any idea on this question?
data storedvalue; set table1; array x{3}; do i=1 to 3; call symputx(cats('macro',put(i,best.)),x{i}); end; run;
As you can see you missed a few points, a set statement, an array statement. Plus you need to create string for the macro name, and refer to the array for the value.
I would also advise you to consider why you want to create lots of macro variables, there is likely a better method.
data storedvalue; set table1; array x{3}; do i=1 to 3; call symputx(cats('macro',put(i,best.)),x{i}); end; run;
As you can see you missed a few points, a set statement, an array statement. Plus you need to create string for the macro name, and refer to the array for the value.
I would also advise you to consider why you want to create lots of macro variables, there is likely a better method.
Depends on the exact problem, the simplest method is just to merge the X row onto any data where you want to use it.
Hi,
I came up with an almost identical solution as @RW9:
data have;
input x1 x2 x3;
datalines;
5 7 6
;
%let macro1=;
%let macro2=;
%let macro3=;
data _null_;
set have;
array x(3);
do i = 1 to 3;
call symputx(cats('macro',i),x[i]);
end;
run;
%put macro1=¯o1;
%put macro2=¯o2;
%put macro3=¯o3;
From the documentation, an advantage of using the cats() function is:
The CATS function removes leading and trailing blanks from numeric arguments after it formats the numeric value with the BESTw. format.
For more details see:
Regards,
Amir.
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.