【问题标题】:SAS - Define macro variable containing undefined macro variable without generating warningSAS - 定义包含未定义宏变量的宏变量而不产生警告
【发布时间】:2017-05-30 20:25:51
【问题描述】:

如何定义一个包含对其他尚未定义的宏变量的引用而不产生警告的宏变量?


考虑一个为不同变量生成相似图的程序。根据变量的不同,每个图形的标签都会发生变化。由于除了特定的分析变量外,所有图形都具有相似的标签,因此将标签放在程序顶部以便于修改是有意义的。问题是,此时程序中的变量名还没有被定义。

例如:

/*Top of program*/
%let label = This &thing gets defined later.;

/* ... */

/*Later in program*/
%let thing = macro variable;
%put &=label;

这会产生所需的输出:

LABEL=This macro variable gets defined later.

但它也会在日志中生成警告:

WARNING: Apparent symbolic reference THING not resolved.

如果我在&thing 周围加上%nrstr,那么label 的形式是正确的(即LABEL=This &thing gets defined later.)但是,&thing 在定义后不再解析。

/*Top of program*/
%let label = This %nrstr(&thing) gets defined later.;
%put &=label;

/* ... */

/*Later in program*/
%let thing = macro variable;
%put &=label;

这个输出:

LABEL=This &thing gets defined later.
LABEL=This &thing gets defined later.

有没有办法避免将警告写入日志?

【问题讨论】:

    标签: sas


    【解决方案1】:

    在这里了解%STR 类型引用和%QUOTE 类型引用之间的区别是有帮助的。

    %QUOTE 及其变体在宏执行 时屏蔽文本,而%STR 及其变体在宏编译 时屏蔽文本。在这种情况下,您关心的是后者,而不是前者,因为您希望 &thing 在执行期间得到解决 - 而不是在编译期间

    所以是 %NRSTR 来救援。您还需要使用 %UNQUOTE 来完全解析宏变量 - 即取消 NRSTR

    /*Top of program*/
    %let label = This %nrstr(&thing.) gets defined later.;
    
    /* ... */
    
    /*Later in program*/
    %let thing = macro variable;
    %put %unquote(&label);
    

    【讨论】:

      【解决方案2】:

      只需在数据步骤中使用 CALL SYMPUTX() 来定义宏变量。

      data _null_;
        call symputx('label','This &thing gets defined later.');
      run;
      /*Later in program*/
      %let thing = macro variable;
      %put &=label;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-05
        • 1970-01-01
        • 1970-01-01
        • 2018-02-23
        • 2016-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多