2017-05-30 117 views
2

如何定義一個宏變量,該變量包含對尚未定義的其他宏變量的引用而不生成警告?SAS - 定義包含未定義宏變量的宏變量而不會產生警告


考慮一個爲不同變量生成類似圖的程序。根據變量的不同,每個圖形的標籤都會改變。由於除了特定的分析變量外,所有圖形都有相似的標籤,所以將標籤放在程序的頂部以便於修改是很有意義的。問題是,在程序中的那一點,變量名稱還沒有被定義。

例如:

/*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. 

是否有某種方式來避免寫警告日誌?

回答

5

以下是瞭解%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); 
0

只需在數據步驟中使用CALL SYMPUTX()來定義宏變量。

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