宏中的 SAS 宏 [英] SAS Macro in macro

查看:37
本文介绍了宏中的 SAS 宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 %macro 的问题.我可以在另一个 %macro 中设置 %macro 吗?

I have one question regaring %macro. Can I set %macro in another %macro?

简短示例 - 情况的图片":

Short example - "picture" of situation:

%macro Tier_1();

   %do Iter = 1 to &i;

       %macro Tier_2()

       proc sql noprint;
         select
         1*&Iter  into :var
       from work._PRODSAVAIL
       ;quit;

        %put &var;

       %mend;
       %Tier_2();
  %end;

%mend;
%Tier_1();

推荐答案

您的问题的答案是是的,这是可能的".但它的风格很差.如果您只是将 %Tier_2 的宏定义移到宏 Tier_1 之外,但将调用保留在其中,则会出现与上述相同的结果.

The answer to your question is "yes, it is possible." But it's poor style. The identical results from above will occur if you simply move the macro definition for %Tier_2 outside of macro Tier_1, but leave the call inside it.

%macro tier_1();

  ... 

  %Tier_2();
%mend tier_1();

%macro tier_2();
  ...
%mend tier_2;

%tier_1();

正如您在上面看到的,您甚至不必以特定方式对它们进行排序 - 只要在宏执行之前编译它们就可以正常工作.

As you see above, you don't even have to order them in a particular way - as long as both are compiled before the execution of the macro it will work fine.

将一个宏定义放入另一个宏定义中的唯一时间是如果外部宏以某种方式修改了内部宏,因此有必要重新编译每次执行外部宏时,内部宏.

The only time it would make sense to put a macro definition inside another macro definition would be if the outer macro modified the inner macro in some way, and so it was necessary to re-compile the inner macro each time the outer macro executes.

虽然这是一个理论上的用例,但我认为您在实践中不太可能遇到;还有很多其他方法可以在不实际修改宏代码本身的情况下修改内容,因此它被认为是糟糕的编程风格,应该避免.您增加了(最小的,但有一些)开销并没有真正的好处,并且使代码更难理解.

While that's a theoretical use case, I don't think it's one you are likely to encounter in practice; there are lots of other ways to modify things without actually modifying the macro code itself, and as such it's considered poor programming style and should be avoided. You're adding (minimal, but some) overhead for no real benefit, and making it harder to understand the code.

这篇关于宏中的 SAS 宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆