2017-03-20 97 views
0

我在SAS中運行這個sql宏。如何用SAS宏解決關鍵字錯誤

%macro calc(table=,cut=,whereclause=); 
proc sql; 
&table 
    select 
     &cut as type format = $40. length = 40 
     ,dt 
     ,count(prod_nbr) as stat 
     ,sum(new) as new 
     ,sum(old) as old 
     ,sum(retired) as retired 
     ,sum(replaced) as replaced 
     ,sum(final) as final 
     ,sum(redo) as redo 

from work.product 
where retail_flg = 1 
&whereclause 
group by 1,2; 
quit; 
%mend calc; 

我在程序中調用宏約六十次,當我調用它時,它大約有80%的時間工作。但每隔一段時間它會產生這個錯誤: ERROR: All positional parameters must precede keyword parameters

如果我以相同的順序運行代碼,錯誤總是顯示在同一行。但是如果我以不同的順序開始運行調用,那麼錯誤最終會發生在調用宏的隨機代碼行上。這裏是調用,它就會趕上上(後計算表已經創建)的一個示例:

%calc(table = insert into calc, cut = 'Product', whereclause = and brand = 'JNJ' and Prod_type = 'N' and index(prod_nm, 'NEW') > 0); 

我特別受錯誤很困惑,因爲我沒有任何位置參數宏。我已經研究過並解決了語法錯誤和其他常見問題而無法解決錯誤。

+0

你怎麼多次調用它?通過其他一些宏?呼叫執行? – Tom

+0

我只是運行上面的代碼的多行代碼。例如: %calc(表格= insert into calc,cut ='Product',where條款=和品牌='JNJ'); %calc(table = insert into calc,cut ='New Produce',whereclause = and brand ='WMT'and Prod_type ='Q'); – Jarom

+0

那你是用手打字的? – Tom

回答

1

很有可能你的某處有不平衡的引號。也許你有WHERECLAUSE的一些價值有不平衡的報價。我會查看生成錯誤消息的值之前的值。它也可能是代碼被截斷的結果。例如,如果將生成的代碼寫入文件,WHERECLAUSE值可能會被截斷並導致不平衡的引號。

在宏調用中添加一些額外的換行符可以更容易地檢查錯誤,因爲較短的行更容易讓人掃描。

%calc 
(table = insert into calc 
,cut = 'Product' 
,whereclause = 
    and brand = 'JNJ' and Prod_type = 'N' 
    and index(prod_nm, 'NEW') > 0 
); 
+0

在我的問題解決中,我每隔一段時間都會看到關於截斷的警告。爲什麼SAS會截斷我的WHERECLAUSE,如何防止它發生? – Jarom

+1

這取決於你如何提交你的代碼。如果您使用SAS Display Manager編輯器,則編輯器可以處理的代碼行數有限制。 – Tom

+0

我認爲這是我的問題。我正在尋找一些解決此問題的方法。你是否有任何解決方案的代碼行太長? – Jarom