2015-11-13 71 views
3

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

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

回答

4

当您使用它们代替通常需要引用的硬编码的非宏文本时,您只需引用宏变量 - 通常在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)); 
+0

好答案。你可能想添加一个'%sysfunc'的例子,这是一个常见的错误。 – Joe

+0

太好了,谢谢 – Victor

+0

@Joe好主意 - 更新。 – user667489