首页 > 解决方案 > 将多个 SAS 数据集写入一张 Excel 工作表

问题描述

我正在使用 SAS 企业指南 7.15。我想将多个数据集导出到多个 excel 表中(每张表中有多个表)。我正在使用 ODS,即使我设置了 sheet_interval="none",但在两个表之后它会打破页面,并将下一个表推到另一个 excel 表中。

这是我的代码示例,这里导出 2 个表,稍后我想在同一张表中再添加 20 个表:

%macro table_1;

    proc report data=table_1 out=sheet_1 headline split='#' spacing=1 nowd missing
    style(summary)={font_weight=bold}
    columns 
    
    segment     
    diff        
    cnt_old         
    cnt_new
    ;
    
    
    compute before _page_/style=[font_size=3 font_weight=bold foreground=white background=Dark Blue];
    line "table 1";
    line ' ';
    line 'Number of Customers';
    endcomp;
    compute after;
    endcomp
    run;

%mend table_1;

%macro table_3;

    proc report data=table_3 out=sheet_1 headline split='#' spacing=1 nowd missing
    style(summary)={font_weight=bold}
    columns 
    
    FinalRiskRating 
    diff
    cnt_old         
    cnt_new;
    
    
    compute before _page_/style=[font_size=3 font_weight=bold foreground=white background=Dark Blue];
    line "table 3";
    
    endcomp;
    compute after;
    endcomp
    run;

%mend table_3;

%table_1; %table_3;


%let shk = table_1 + table_3;
ods path work.temptemp(update) sasuser.templat(update) sashelp.tmplmst(read);
ods path show;
Options mprint mlogic symbolgen nobyline;
ods listing close;

%macro b;

    %do i=1 %to 2;
    %let mshk=%scan(&shk., &i.,+);
    /*ods tagsets.excelxp options(SHEET_INTERVAL='NONE' PAGEBREAK="NO"  sheet_name="sheet1" ABSOLUTE_COLUMN_WIDTH='8' AUTOFIT_HEIGHT='yes' embed_titles_once = 'yes' embedded_titles='yes');*/
    ods tagsets.excelxp options(sheet_interval="none" sheet_name="sheet1" ABSOLUTE_COLUMN_WIDTH='8' AUTOFIT_HEIGHT='yes' embed_titles_once = 'yes' embedded_titles='yes');
    %&mshk.;
    %end;

%mend b;

ods tagsets.excelxp file="&reportlocation";

%b;
ods tagsets.excelxp close;
ods _all_ close;
;

标签: sasexport-to-excelsas-macroenterprise-guidesas-dis

解决方案


我的怀疑是因为您没有sheet_interval='none'在初始ods tagsets.excelxp.

和你一样,第一个例子有这个问题:

ods tagsets.excelxp  file="h:\temp\test.xls";
ods tagsets.excelxp options(sheet_interval='none');
proc print data=sashelp.class;
run;
ods tagsets.excelxp options(sheet_interval='none');
proc print data=sashelp.class;
run;
ods tagsets.excelxp close;

但这按预期工作:

ods tagsets.excelxp options(sheet_interval='none') file="h:\temp\test.xls";
proc print data=sashelp.class;
run;
proc print data=sashelp.class;
run;
ods tagsets.excelxp close;

有趣的是,如果您删除第二个,它仍然有效 - 所以它并不完全不在第一行,而是它是新的选项语句。我想这会以某种方式重置它。但在你的情况下,真的没有理由不将任何这些细节放在最初的ods tagsets.excelxp声明中。

ODS EXCEL不是那样工作的,它总是在一张纸上。如果可以的话,我建议在任何情况下都使用它。


推荐阅读