简体   繁体   English

如何在循环SAS中的数据步骤中为宏分配值

[英]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. 我正在尝试将i的值(1到10)分配给宏变量var1。 In the same datastep, I will use the var1 to index arrays. 在同一数据步骤中,我将使用var1索引数组。

You can change your call symput to this: 您可以将call symput更改为此:

 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. 我还把put()包裹在第二个参数上,以使用symput来清除日志消息。

(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. 编辑 :内特(Nate)很好地了解了在同一数据symput无法使用symput创建的SAS变量。 If you still want to go with this solution, you can move the symput loop into a _null_ datastep before your "temp1" step. 如果仍然想使用此解决方案,则可以在“ temp1”步骤之前将symput循环移到_null_ datastep中。

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. 一种是不能同时使用调用symput创建宏变量并在同一数据步骤中引用它,因此除非将其拆分为多个数据步骤,否则调用symput的任何变化都无法解决。

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. 另一个是,在编写代码时(以及在描述中),您将要重写10次“ var1”……您不是要&var1吗? , &var2. ,&var2。 , ... , &var10. ,...,&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? 为什么不简单地将x_1写到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. 也许如果您向我们提供有关您要完成的目标的更多信息,我们可以为您提出更好的策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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