如何在不产生警告的情况下定义一个宏变量,该宏变量包含对尚未定义的其他宏变量的引用?
考虑一个为不同变量生成相似图的程序。根据变量,每个图形的标签将更改。由于所有图形都具有相似的标签,除了特定的分析变量外,将标签放置在程序顶部以便于修改很有意义。问题在于,此时在程序中尚未定义变量名。
例如:
/*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.
如果我将
%nrstr
放在&thing
周围,则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.
有什么方法可以避免将警告写入日志?
最佳答案
在这里,了解%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);