简体   繁体   中英

How to assign a value to a macro in datastep in a loop SAS

So I am trying to do something like this:

data temp1;
   set temp;
   do i=1 to 10;
     call symput("var1", i);
   end;

 array x(*) x_&var1 to x_&var10;
    ....................


run;

I am trying to assign the value of i (1 to 10) to macro variable var1. In the same datastep, I will use the var1 to index arrays.

You can change your call symput to this:

 call symput(compress('var'||put(i,8.)), put(i,8.));

I also wrapped a put() around the second argument to symput to clean up a log message.

(This does seem like a weird pattern though. If you're dynamically creating a lot of macro variables, you might be able to rethink your overall strategy and come up with something simpler.)

Edit : Nate has a good point about SAS variable created with symput not being usable inside the same datastep. If you still want to go with this solution, you can move the symput loop into a _null_ datastep before your "temp1" step.

There's a few issues here.

One is that you can't both create a macro variable with call symput and reference it within the same data step, so no variation on call symput is going to fix that unless you split it into multiple data steps.

Another is that, as you have your code written (and in your description), you'll be re-writing "var1" 10 times...do you not instead want &var1. , &var2. , ... , &var10. ? If so, I would recommend something like:

call symput('var'||left(i),i);

Finally, if you only need these macro variables for the array declaration, why do you need to use macro variables here at all? Why not simply write x_1 to x_10? Maybe if you give us more information about what precisely you're trying to accomplish we can come up with a better strategy for you to go about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM