這篇文章用于宏程序備份坯约。
在使用ods excel
語句批量將SAS數(shù)據(jù)集輸出到EXCEL時藐握,發(fā)現(xiàn)記錄數(shù)為0空數(shù)據(jù)集無法成功輸出到EXCEL中姐军。于是刹缝,寫了個宏程序丢间,用于為空數(shù)據(jù)集添加一行記錄說明。
這個宏程序的功能雖然并不常用亩鬼,但宏程序的處理思路可供參考借鑒殖告。宏程序主要分為三部分:
- 獲取輸入數(shù)據(jù)集的邏輯庫名稱和數(shù)據(jù)集名稱;
- 判斷數(shù)據(jù)集是否存在辛孵,以及記錄數(shù)是否為0丛肮;
- 為記錄數(shù)為0的數(shù)據(jù)集添加一行說明赡磅。
演示代碼如下:
/*Give a test*/
data test;
length a b $200;
stop;
run;
%empty_des(dt = test);
結(jié)果如下:
匯總代碼如下:
%macro empty_des(
dt= /*Input data set*/
);
**Get libname and dataset name;
%local libname dataset;
%if %index(&dt., .) %then %do;
%let libname = %scan(&dt, 1, .);
%let dataset = %scan(&dt, 2, .);
%end;
%else %do;
%let libname = WORK;
%let dataset = &dt.;
%end;
%put libname = &libname.;
%put dataset = &dataset.;
**Add description for dataset with no record;
%if %sysfunc(exist(&libname..&dataset.)) %then %do;
proc sql noprint;
select strip(put(nobs, best.)) into: nobs_&dataset.
from dictionary.tables
where libname = upcase("&libname.") and memname = upcase("&dataset.");
quit;
%put nobs_&dataset. = &&nobs_&dataset.;
%if &&nobs_&dataset. = 0 %then %do;
data empty;
length DES $200;
des = "There is no record in this dataset.";
output;
run;
data &libname..&dataset.;
length DES $200;
set empty &libname..&dataset.;
run;
proc delete data = work.empty;
run;
%put The dataset &libname..&dataset.has 0 record and description added.;
%end;
%else %do;
%put The dataset &libname..&dataset.has more than 1 record and no description added.;
%end;
%end;
%else %do;
%put The dataset &libname..&dataset. does not exist.;
%end;
%mend empty_des;
/*Give a test*/
data test;
length a b $200;
stop;
run;
%empty_des(dt = test);
感謝閱讀魄缚, 歡迎關(guān)注:SAS茶談!
若有疑問焚廊,歡迎評論交流冶匹!