代码之家  ›  专栏  ›  技术社区  ›  Martin Bøgelund

SAS宏包含保护

  •  3
  • Martin Bøgelund  · 技术社区  · 15 年前

    在诸如C++等其他编程语言中,包含保护器以防止相同代码的多个包含。

    这样在C++中:

    #ifndef FOO_INCLUDED
    #define FOO_INCLUDED
    ....
    #endif
    

    在SAS宏函数定义中构建包含保护是否有意义?怎么做呢?

    2 回复  |  直到 15 年前
        1
  •  3
  •   Chang Chung    15 年前

    %SYMEXIST(macro-var-name) 宏函数查看宏变量是否存在,但不能写入 %IF 在露天,所以你必须把你的 %IF 语句在其他宏中。您可能最终会编写一个宏,只是为了将代码包装在如下所示的源文件中。这不是很漂亮,但如果需要警卫的话,你也许能挺过去。

    %macro wrapper;
      %if %symexist(foo_defined) %then %return;
      %macro foo;
        %global foo_defined;
        %let foo_defined = 1;
        %put i am foo; 
      %mend foo;
    %mend  wrapper;
    
    %*-- tests --*;
    options mcompilenote=all;
    %symdel foo_defined;
    
    %*-- first time it will define %foo --*;
    %wrapper
    %foo
    /* on log
    NOTE: The macro FOO completed compilation without errors.
          6 instructions 108 bytes.
    i am foo
    */
    
    %*-- second time it will not --*;
    %wrapper
    %foo
    /* on log
    (no notes on macro compilation)
    i am foo
    */
    

    在调用时,SAS提供了一组目录、文件和目录,用于访问(编译/未编译)宏。这使得直接查明给定宏名称的宏是否已经可用于此会话非常麻烦,但并非不可能。阅读本文中的(血腥)详细信息: http://support.sas.com/resources/papers/proceedings09/076-2009.pdf

        2
  •  2
  •   Ville Koskinen    15 年前

    你可以用 NOMREPLACE option 以防止重新定义任何宏。

    在我看来,重用宏名和宏变量名(甚至数据集名)是有害的。如果您只定义了一次,那么您可以相对地确定您可以重新提交代码的任何部分,并期望得到与最初相同的结果。我还喜欢将宏定义与调用它们的代码分开。