Excel SAS将列的子集导出到具有列名的工作表

Excel SAS将列的子集导出到具有列名的工作表,excel,sas,export,Excel,Sas,Export,给定一个SAS数据集,其列名为n1、n2、…nN 是否有一种简单的方法可以将公共列集和唯一列子集导出到工作簿中,在工作簿中,每一列都以与上一列名称相同的名称导出到工作表中 例如: 对于上面的SAS数据集,列: n1、n2、n5->工作表n5 n1、n2、n9->工作表n9 n1、n2、n13->工作表n13 导出到Excel工作簿,工作表的名称如上所述 感谢您的建议。在过程导出步骤中使用SHEET=语句 例如: filename myxl 'c:\temp\sandbox.xlsx'; pro

给定一个SAS数据集,其列名为n1、n2、…nN

是否有一种简单的方法可以将公共列集和唯一列子集导出到工作簿中,在工作簿中,每一列都以与上一列名称相同的名称导出到工作表中

例如:

对于上面的SAS数据集,列:

n1、n2、n5->工作表n5 n1、n2、n9->工作表n9 n1、n2、n13->工作表n13 导出到Excel工作簿,工作表的名称如上所述

感谢您的建议。

在过程导出步骤中使用SHEET=语句

例如:

filename myxl 'c:\temp\sandbox.xlsx';

proc export replace file=myxl dbms=excel 
  data=sashelp.class (keep=name)
;
  sheet='Name';
run;

proc export replace file=myxl dbms=excel
  data=sashelp.class (keep=name age weight) 
;
  sheet='Weight';
run;
可以对宏进行编码以生成重复零件

%macro excel_push (file=, data=, always=, each=);
  %local i n var;
  %let n = %sysfunc(countw(&each));
  %do i = 1 %to &n;
    %let var = %scan(&each,&i);

    proc export replace file=&file dbms=excel 
      data=&data(keep=&always &var)      
    ;
      sheet="&var";
    run;

  %end;

%mend;

options mprint;

filename myxl2 'c:\temp\sandbox2.xlsx';

%excel_push (
  file=myxl2,
  data=sashelp.class,
  always=name age sex,
  each=height weight
)
如果打开Excel输出,将其保持打开状态并重新运行代码,则会出现一个错误,尽管有点模糊:

ERROR: Error attempting to CREATE a DBMS table. ERROR: Execute: The Microsoft Access database
       engine could not find the object ********. Make sure the object exists and that you spell
       its name and the path name correctly. If ******** is not a local object, check your
       network connection or contact the server administrator..

我想,我从您的问题中了解到的是,如何将工作表名称与SAS数据集的最后一个变量关联起来。一种方法是使用dictionary.columns,并在数据集中找到dictionary.columnsis max中的哪一列位置varnum,该数据集将给出最后一个变量,您可以从中生成一个宏变量,并将其用于proc export中的工作表

/* pick up the last variable*/
 proc sql ;
select name into :mysheet TRIMMED from dictionary.columns
where libname = "SASHELP"
and memname = "CLASS"
and  varnum = (select max(varnum) from dictionary.columns
             where libname = "SASHELP"
            and memname = "CLASS");

/* use the macrovariable in your sheet statement*/

 PROC EXPORT DATA= Sashelp.Class  /*Sheet 1*/
     outfile= "/folders/myfolders/class.xlsx "
     dbms=xlsx replace;
     sheet="&mysheet";
run;

很不错的。我设计的宏更笨重。非常感谢。如果最后几列都是相同的类型、字符或数字,您可以转换数据,并使用ODS EXCEL和分组将其完全数据驱动。与此类似: