2014-11-24 65 views
1

我被卡住,試圖迭代宏%do循環中的值列表。每個值都應該用作變量後綴。在迭代列表時創建後綴

該方法是基於SAS文檔:http://support.sas.com/kb/26/155.html

的(簡化的)代碼是:

%macro loop(values);       
    %let count=%sysfunc(countw(&values)); 
    %do i = 1 %to &count;   
     %let value=%qscan(values,i,%str(,)); 
     proc sql; 
      select count(distinct hut_id) as prefix_&value. 
      from saslib.tl1_results_eval 
      group by plan_cell; 
     quit; 
    %end;                    
%mend;     
%loop(%str(a,b,c,d)) 

所得錯誤信息是:

MLOGIC(LOOP): %DO loop beginning; index variable I; start value is 1; stop value is 4; by value 
     is 1. 
MLOGIC(LOOP): %LET (variable name is VALUE) 
MPRINT(LOOP): proc sql; 
22: LINE and COLUMN cannot be determined. 
NOTE 242-205: NOSPOOL is on. Rerunning with OPTION SPOOL might allow recovery of the LINE and 
       COLUMN where the error has occurred. 
ERROR 22-322: Syntax error, expecting one of the following: ',', AS. 
MPRINT(LOOP): select count(distinct hut_id) as prefix_a from group by plan_cell; 
MPRINT(LOOP): quit; 

有趣的是,如果刪除「count(distinct hut_id)」中的「prefix_」作爲前綴_ &值。「 - 它工作得很好。不幸的是,我確實需要那裏的前綴。另外,正如您在日誌消息中看到的那樣,PROC SQL語句最終得到了正確編譯,但代碼在執行之前出錯。

任何想法發生了什麼?謝謝!

回答

1

由於qscan功能,您正在插入不可見的引號字符。嘗試:

%macro loop(values);     
    proc sql;  
    %let count=%sysfunc(countw(&values)); 
    %do i = 1 %to &count;   
     %let value=%qscan(values,i,%str(,)); 
     select count(distinct hut_id) as prefix_%unquote(&value) 
     from saslib.tl1_results_eval 
     group by plan_cell; 
    %end; 
    quit;                
    %mend;     
    %loop(%str(a,b,c,d)) 

注意proc sql;語句的運動 - 很少有很好的理由退出;並重新實例化每個SQL語句之間的SQL過程,從而導致開銷。

+0

您可以擴展這些'好理由'嗎?謝謝 – 2014-11-24 19:56:43

+0

要獲得每個步驟的計時統計信息,並且還希望在發生錯誤後執行後續的SQL語句。 – 2014-11-24 20:57:34

+0

工作起來就像一個魅力RawFocus。非常感謝! – 2014-11-25 09:20:43