BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
gxu
Calcite | Level 5 gxu
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Peter_C
Rhodochrosite | Level 12

&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

4 REPLIES 4
Peter_C
Rhodochrosite | Level 12

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

gxu
Calcite | Level 5 gxu
Calcite | Level 5

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

Tom
Super User Tom
Super User

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;

gxu
Calcite | Level 5 gxu
Calcite | Level 5

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

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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