2011-06-21 38 views
3

在乾淨的會話本地執行:SAS宏引用如何與格式文字進行交互?

%let x = %str(put(age, best.)); 

proc sql; 
    select &x from sashelp.class; 
quit; 

這會產生以下錯誤:

1  put(age, best.) 
       ---- 
       22 
       ---- 
       76 
ERROR 22-322: Syntax error, expecting one of the following: a format name, ?. 

ERROR 76-322: Syntax error, statement will be ignored. 

但這種 「手動解決」 的版本沒有票據,警告或錯誤運行:

proc sql; 
    select put(age, best.) from sashelp.class; 
quit; 

有人可以解釋在這個程序中%str()在做什麼時會造成問題嗎?對這個模糊的問題抱歉,但我不確定相關的相互作用是什麼;我無法使用等效的數據步語法進行復制,因此可能涉及到SQL特性。

回答

2

回答在this question on runsubmit.com

I'm going to mark this answer as correct because it led me to this page of documentation: http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#tw3514-unquote.htm - "In rare cases, masking text with a macro quoting function changes the way the word scanner tokenizes the text ... The word scanner does not use it as the boundary of a literal token in the input stack". Sounds like a bug, frankly, but if the tokenizer algorithm is as ancient and hairy as I imagine, I'd spin it as a quirk too!

3

%str()函數在宏編譯過程中屏蔽了一個字符串。刪除let語句中的%str()函數,或者在sql select中添加%unquote()函數以正確解析。

+0

但爲什麼會掩蓋宏編譯過程中的字符串會導致這個錯誤?我希望所有的宏引用都能在代碼執行的時候解決。 – jl6

+1

%str在編譯期間屏蔽了字符串,而其兄弟%引號在執行期間屏蔽了字符串。這是proc sql,宏編譯和執行的結合。本文解釋了很多關於宏觀報價的內容:http://www2.sas.com/proceedings/sugi28/011-28.pdf –

1

您可以使用格式語句嗎?例如,這工作得很好。

%let x = %str(age format=best.); 

proc sql; 
    select &x. from sashelp.class; 
quit; 
+0

這個例子有點人爲;我知道任何數量的解決方法,所以我真正問的是爲什麼會發生此錯誤。 – jl6

1

由於某些原因,SAS不喜歡「最好」。格式。

即當我嘗試這一點,你的代碼工作

%let x = %str(put(age, 8.)); 

????

0

如果您添加到您的代碼

%put _user_ ; 

你將看到如何& x由海峽%引述,在日誌中。這就是爲什麼proc sql代碼不起作用。在proc sql語句的select部分中使用%Unquote將允許代碼運行。

http://www2.sas.com/proceedings/forum2007/152-2007.pdf

+0

這真的很有用 - 謝謝。我認爲你需要使用'%put'而不是'put'。 –

+0

謝謝,更新 –