最新的SAS tech report中提到了一個小撇步

將macro用在DATA step,在這段過程中使用PROC DATASETS目標檔案刪除

在macro裡面應用call execute、%index與%scan函數

可以將此視為DATA step與macro的call execute綜合範例

初學者可參考此程式,體會一下在DATA step內使用call execute的感覺

回想一下call execute,顧名思義就是呼叫執行某個動作

而這個功能主要用在DATA step內作執行,寫法很簡單

call execute ( ‘ argumet ‘ )

範例可參照:[SAS] macro_使用CALL EXECUTE

直接來看程式

%macro deldsn(dsn);/*建立名稱為deldsn的macro*/                                                                                                               
     %if %index(&dsn,.) eq 0 %then %do;/*使用IF-THEN-ELSE條件式篩選*/                                                                                             
      call execute(‘proc datasets nolist;’);/*寫入欲執行的程式行,nolist為不輸出*/                                                                       
      call execute(“delete &dsn;”);/*設定欲刪除的檔案名稱*/                               
      call execute(‘quit;’);/*注意以quit作結*/                                                                                  
 %end;                                                                                                                                 
 %else %do;/*當不符合條件時,利用scan辨認&dsn以點分割第二位置的值*/                                                                                             
      call execute(“proc datasets lib=%scan(&dsn,1,.) nolist;”);                                                                           
      call execute(“delete %scan(&dsn,2,.);”);                                                                                             
      call execute(‘quit;’);                                                                                                               
 %end;                                                                                                                                 
%mend deldsn; /*養成習慣在%mend後寫macro名稱, 方便辨識*/

其實可將這段macro簡言之是希望在遇到不同狀況時,給予不同的datasets procedure處理

將之拆成兩個部分來看:

第一部分「%index(&dsn,.) eq 0」即表示這個檔案名稱是屬於暫存檔,反之就是永久檔

因此第二部分「%scan(&dsn,1,.)」就是在抓Library的名稱

以此推「 %scan(&dsn,2,.);」,就知道是抓Library後面的檔案名了吧!

寫了這段macro後,當然不是擺著好看…..

data test.one;                                                                                                                             
     y=200;                                                                                                                                
     %deldsn(test.one)                                                                                                                         
     x=100;                                                                                                                                
run; 

/***********   該LOG檔案顯示  *************/

NOTE: CALL EXECUTE generated line.
1   + proc datasets lib=test nolist;
2   + delete one;
3   + quit;

NOTE: Deleting TEST.ONE (memtype=DATA).
NOTE: PROCEDURE DATASETS used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds

/**********  若執行mprint來看  ************/

70   data test.one;
71    y=200;
72    %deldsn(test.one)
MPRINT(DELDSN):   call execute(“proc datasets lib=test nolist;”);
MPRINT(DELDSN):   call execute(“delete one;”);
MPRINT(DELDSN):   call execute(‘quit;’);
73    x=100;
74   run;

因為「%index(&dsn,.) eq 0」偵測到test.one出來的index結果不為零,因此執行else的datasets procedure

根據使用者的需求,看是要刪除永久檔或暫存檔

若要刪除暫存檔,%deldsn(one)就可以達到目的了!

參考來源:Function like macro that deletes a data set from within a DATA step,  http://support.sas.com/kb/43/814.html


Comments



You must be logged in to post a comment.

Name (required)

Email (required)

個人網站

Speak your mind

total of 3715445 visits