2009-09-04 66 views
2

我正在使用SAS 9.1.3在DATA步驟中調用一個宏,但該宏生成一個PROC REPORT步驟,所以我使用CALL EXECUTE調用它,生成所有這些PROC REPORT步驟,然後全部執行它們DATA步驟後。我可以更改SAS中的CALL EXECUTE堆棧的執行順序嗎?

我使用數組,宏每次執行這個陣列中的每個元素:

DATA macro_test; 
    ARRAY questions[3] $ 32 ('question1' 'question2' 'question3'); 

    DO i=1 to 3; 
    a_question = questions(i); 
    CALL EXECUTE("%report_by_question(a_question)"); 
    end; 

RUN; 

的事情是,報告輸出出來(通常情況下)向後 - 它將打印question3先,然後是2,然後是1.

有沒有辦法修改CALL EXECUTE的執行順序,這樣我就可以按順序打印問題報告,還是隻是做自己的事情?

謝謝!

回答

5

我假定你的意思是更多像這樣YOUT call execute()行:

CALL EXECUTE("%report_by_question(" || trim(left(a_question)) || ");"); 

與測試宏我得到一些日誌行像這樣,顯示出call execute() s的正確的順序發生。你有類似的東西嗎?

%macro report_by_question(a); 
data test_&a; 
    do i=1 to 10000000; 
    output; 
    end; 
run; 
%mend; 

日誌

 
NOTE: CALL EXECUTE generated line. 
1 + data test_question1; do i=1 to 10000000;  output; end; run; 

NOTE: The data set WORK.TEST_QUESTION1 has 10000000 observations and 1 variables. 
NOTE: DATA statement used (Total process time): 
     real time   6.14 seconds 
     cpu time   0.45 seconds 


1 +                 ; 
2 + data test_question2; do i=1 to 10000000;  output; end; run; 

NOTE: The data set WORK.TEST_QUESTION2 has 10000000 observations and 1 variables. 
NOTE: DATA statement used (Total process time): 
     real time   3.87 seconds 
     cpu time   0.53 seconds 


2 +                 ; 
3 + data test_question3; do i=1 to 10000000;  output; end; run; 

NOTE: The data set WORK.TEST_QUESTION3 has 10000000 observations and 1 variables. 
NOTE: DATA statement used (Total process time): 
     real time   3.12 seconds 
     cpu time   0.45 seconds 
+0

沒錯,哈哈,忘了對變量的整體串連;) 所以,是的,我得到的日誌中生成的步驟的正確順序,但是當他們開始在輸出被吐了出來,他們是看起來完全隨機的順序(我現在使用5個問題作爲測試)。 – chucknelson 2009-09-04 15:10:54

+0

好吧 - 我只是做了一個測試,並將ODS轉換爲HTML,並且報表按照正確的順序排列。我不明白爲什麼ODS LISTING會以不同的順序將它們輸出到輸出中。 – chucknelson 2009-09-04 15:15:06

+0

呼叫執行的工作正常,爲了 - 只是包裝這個問題! – chucknelson 2009-09-30 21:31:32

1

我喜歡做的一切宏中使用宏語言有關。我想這個權衡是你的程序中散佈了很少的宏。但是,爲了防止程序生成報告,只需註釋掉宏調用(*%loopit;),您不必輸入「question1」,「question2」,「question3」等!
希望這對你有用!

%macro report_by_question(input); 
    %put "Question: " &input; 
%mend; 

%macro loopit; 
    %do i=1 %to 3; 
     %report_by_question("question&i."); 
    %end; 
%mend loopit; 
%loopit; 
+0

感謝您的洞察力,在SAS中還沒有涉及到「宏語言」;)上面的這個例子只是簡化來解釋我的問題。我正在使用DATA步驟來循環遍歷事物,並讀取變量/問題數據集 - 而不是對這些東西進行硬編碼,哈哈。 – chucknelson 2009-09-04 20:39:27

2

數據步驟被編譯,然後執行。 call execute(str);將str推入輸入隊列,以便在之後彈出數據步驟完成執行。訂單保存期限。但是,如果您將宏調用放在雙引號字符串中,如下所示: call execute(「%report(q)」); 然後宏在編譯數據步驟時被調用,甚至在數據步驟開始運行之前。

如果您不想在編譯時調用宏,則可以引用宏或將其放入單引號字符串中。下面是一個例子。希望這可以幫助。

/* create a dataset with 1 var and 3 obs */ 
data qs; 
    input q $; 
cards; 
q1 
q2 
q3 
; 
run; 

/* reporting macro -- a mockup */ 
%macro report(q=); 
    %put report for q=&q; 
%mend report; 

/* call the reporting macro for each q */ 
data _null_; 
    set qs;  
    macro = catx(q, '%report(q=', ')'); 
    call execute(macro); 
run; 

/* on log 
report for q=q1 
report for q=q2 
report for q=q3 
*/ 


/* to show what happens when the 
    macro is invoked during the compile 
    time */ 
data _null_; 
    call execute("%report(q=q1)"); 
    put "after call execute"; 
run; 
/* on log 
1 data _null_; 
2  call execute("%report(q=q1)"); 
report for q=q1 
3  put "after call execute"; 
4 run; 
after call execute 
*/ 
相關問題