何时引用宏变量

When to quote a macro variable

在 SAS 中,当引用宏变量时,我发现有时代码在宏引用周围使用双引号,但其他时候,它引用的变量不带引号。

我什么时候应该在 & 之前使用引号,什么时候不应该?

当您使用宏变量代替通常需要引用的硬编码非宏文本时,您只需要引用宏变量 - 通常在 proc 或数据步骤代码中,例如

%let text = Hello;

/*You need quotes here*/
data _null_;
put "&text";
run;

/*Otherwise you get an error, because SAS thinks hello is a variable name*/
data _null_;
put &text;
run;

/*If you already have quotes in your macro var, you don't need to include them again in the data step code*/
%let text2 = "Hello";
data _null_;
 put &text2;
run;

/*In macro statements, everything is already treated as text, so you don't need the quotes*/
%put &text;

/*If you include the quotes anyway, they are counted as part of the text for macro purposes*/
%put "&text";


/*When you are calling functions via %sysfunc that normally
require a variable name or a quoted string, you don't need quotes*/
%let emptyvar=;
%put %sysfunc(coalescec(&emptyvar,&text));