这篇文章用于宏程序备份。
在使用ods excel
语句批量将SAS数据集输出到EXCEL时,发现记录数为0空数据集无法成功输出到EXCEL中。于是,写了个宏程序,用于为空数据集添加一行记录说明。
这个宏程序的功能虽然并不常用,但宏程序的处理思路可供参考借鉴。宏程序主要分为三部分:
- 获取输入数据集的逻辑库名称和数据集名称;
- 判断数据集是否存在,以及记录数是否为0;
- 为记录数为0的数据集添加一行说明。
演示代码如下:
/*Give a test*/
data test;
length a b $200;
stop;
run;
%empty_des(dt = test);
结果如下:
汇总代码如下:
%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);
感谢阅读, 欢迎关注:SAS茶谈!
若有疑问,欢迎评论交流!
网友评论