Hi All,
I have a question regarding to the macro variable in a variable name.
Basically, I want to decide an index which can be calculated from i and j, then I will use this index to create a variable name.
For example, in the following code, I expect the dataset doudou to contain a variable a6=1 when i=1, j=2, but it didn't work.
Do you have any idea how to solve this question?
%let j=2;
%let ind=(&i-1)*18+(&j-1)*6;
data doudou;
a&ind=1;
run;
Thanks for your attention!
xgn
&ind is a string
OK it contains an expression
when you want it evaluated, you need to do something
otherwise
a&ind is passed to the compiler as
a(&i-1)*18+(&j-1)*6
Then it has to decide if a( implies an array reference or a (probably user) function.
If you wanted
a6
then you must encourage the evaluation of the expression in &ind
Perhaps %eval( &ind ) might achieve what you want
as in
23 %let i=1;
24 %let j=2;
25
26 %let ind=(&i-1)*18+(&j-1)*6;
27
28 data doudou;
29 a%eval(&ind)=1;
30 putlog _all_ ;
31 run ;
a6=1 _ERROR_=0 _N_=1
NOTE: The data set WORK.DOUDOU has
OK?
&ind is a string
OK it contains an expression
when you want it evaluated, you need to do something
otherwise
a&ind is passed to the compiler as
a(&i-1)*18+(&j-1)*6
Then it has to decide if a( implies an array reference or a (probably user) function.
If you wanted
a6
then you must encourage the evaluation of the expression in &ind
Perhaps %eval( &ind ) might achieve what you want
as in
23 %let i=1;
24 %let j=2;
25
26 %let ind=(&i-1)*18+(&j-1)*6;
27
28 data doudou;
29 a%eval(&ind)=1;
30 putlog _all_ ;
31 run ;
a6=1 _ERROR_=0 _N_=1
NOTE: The data set WORK.DOUDOU has
OK?
Peter,
Yes, it works, thank you! P.S, I didn't know %eval before, it seems it's very useful for my future work.
Best regards,
xgn
Probably this code is just a simplified version of what you are really doing, but it does not look like an application for a macro arithmetic.
Seems it would be more appropriate to just write it in SAS rather then using the macro language to generate SAS code.
data doudou;
i=1;
j=2;
set mydata;
array a a:;
a((i-1)*18+(j-1)*6)=1;
run;
Or perhaps you need a two dimension array?
data doudou;
i=1;
j=2;
set mydata;
array a(18,6) a:;
a(i,j)=1;
run;
Hi Tom,
You are right. The above code is just a simple version of what I am going to do. I will also try your methods. Thank you so much!
Best regards,
xgn
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.