在創(chuàng)建數(shù)據(jù)集的時候,有時需要判斷數(shù)據(jù)集是否存在。
在SAS的幫助文檔找到方法;
通過定義,并調(diào)用宏的方式實現(xiàn)。
定義一個宏:
%macro checkds(data_set);
%if %sysfunc(exist(&data_set)) %then %do;
%put "Data set &data_set. is already exists !";
%end;
%else %do;
%put "Data set &data_set. does not exist !";
run;
%end;
%mend checkds;
- 請求宏:
%checkds(work.a)
- 日志界面會打?。合鄳?yīng)指定的輸出。
"Data set work.a does not exist !"
- 創(chuàng)建數(shù)據(jù)集
data a;
a=1;
run;
- 請求宏:
%checkds(work.a)
- 日志
"Data set &data_set. is already exists !"
擴展使用,宏是SAS編程過程中的常用技術(shù),[%sysfunc]中可以使用SAS中大部分內(nèi)置函數(shù),不過[%if]后可以編寫自定義函數(shù),且將[ %put "...!";]替換成自需要的data step 或者 proc step,已達到使用的目的。