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
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.
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.