在《sas应用之批量拆分生成数据集及导出》中有列举该应用,当时是分了几个步骤,容易产生导出的excel文件名称和其内容不一致的情况。在这我们将这样的过程放在一个宏中去实现,使用%do循环,在每个循环中完成建立一个数据集和对应excel文件导出。程序如下:
/*建立test41逻辑库和数据集存储路径*/
%let path=D:\develop\SAS\41数据输出之excel;
libname test41 "&path.\data";
options user=test41;
/*创建宏*/
%macro b(group,dset);
/*使用sql创建两个宏变量,一个是存储类别名称,一个类别的个数*/
proc sql noprint;
select distinct &group.into : m_group
separated by ',' from &dset.;
select count(distinct &group.) into :
m_group_cnt from &dset.;
quit;
/*使用循环语句创建数据集和导出excel文件*/
%do i=1%to &m_group_cnt.;
%let group_name=%scan(%bquote(&m_group.),&i.,%str(,));
%put &group_name.;
data &group_name.;
set &dset.;
where&group. in ( "&group_name.");
run;
proc export data=&group_name
outfile="&path.\result\数据输出test.xlsx"
DBMS=excel REPLACE;
sheet="&group_name";
RUN;
%end;
%mend;
%b(LEVEL,sashelp.Adsmsg);
我们知道office2010最大存储行为1048576,列为16384.当导出的行太多需要事先做个判断,在一个表单中是否能容纳,否则拆分表格或者考虑其他导出格式,如.sav等。
更多精彩请关注“sas应用分析”微信公众号,sas base,advanced和clinical trial认证资料免费送。