DATA Step, Macro, Functions and more

How to resolve Macro Variable as Part of Variable Name

Accepted Solution Solved
Reply
Occasional Contributor gxu
Occasional Contributor
Posts: 18
Accepted Solution

How to resolve Macro Variable as Part of Variable Name

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 i=1;

%let j=2;

%let ind=(&i-1)*18+(&j-1)*6;

data doudou;

a&ind=1;

run;

Thanks for your attention!

xgn


Accepted Solutions
Solution
‎12-06-2011 04:41 PM
Valued Guide
Posts: 2,177

How to resolve Macro Variable as Part of Variable Name

&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?

View solution in original post


All Replies
Solution
‎12-06-2011 04:41 PM
Valued Guide
Posts: 2,177

How to resolve Macro Variable as Part of Variable Name

&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?

Occasional Contributor gxu
Occasional Contributor
Posts: 18

How to resolve Macro Variable as Part of Variable Name

Peter,

Yes, it works, thank you!  P.S, I didn't know %eval before, it seems it's very useful for my future workSmiley Happy.

Best regards,

xgn

Super User
Super User
Posts: 7,070

Re: How to resolve Macro Variable as Part of Variable Name

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;

Occasional Contributor gxu
Occasional Contributor
Posts: 18

Re: How to resolve Macro Variable as Part of Variable Name

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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