SAS 宏:将表导出到一个 Excel 文件中的不同工作表

发布于 2025-01-18 02:15:03 字数 964 浏览 3 评论 0原文

SAS初学者。我制作了一个宏,可以从不同库的数据集创建表(每年都有一个库)。 PROC列表零件正常工作。我的问题是,我想将每个表格导出到相同 excel文件中的一张纸上,并用宏变量标记为“年”。最后,我想拥有一个名为20、19、18、17、16、15和14的excel文件。

我通过插入ods tagsets.excelxp命令来尝试此操作,因为这是我“继承”的整个语法中也使用的导出命令。 结果是一个excel文件,其中一个名为“&年”的表格。 (不是“ 14”)以及最后一个宏观呼叫的数据,第14年。

有人知道如何解决吗?另外,我很高兴提出有关一种比打电话7次更优雅的方式的建议。就像在数年之类的清单中循环。

   %macro MyMacro (year = );
    %local indsn;
    %let indsn=path&year..myfile1;
    
ods tagsets.excelxp File='P:\Folder\Folder\Folder\MyMacro.xls'  style=statistical options(sheet_name='&year.'); 

    proc tabulate data=&indsn;
    class var1 var2;
    table var1 all, var2 all/printmiss;
    title "var1 and var2, year: &year.";
    run;

ods tagsets.excelxp close;
    
    %mend MyMacro;
    
    %MyMacro(year=20)
    %MyMacro(year=19)
    %MyMacro(year=18)
    %MyMacro(year=17)
    %MyMacro(year=16)
    %MyMacro(year=15)
    %MyMacro(year=14)

SAS beginner here. I made a macro that creates tables from datasets of different libraries (there is one library for each year). The proc tabulate part works fine. My problem is that I would like to export each table to one sheet within the same Excel file, and each sheet labeled with the macro variable "year". In the end I would like to have one Excel file with sheets named 20, 19, 18, 17, 16, 15 and 14.

I tried this by inserting an ods tagsets.excelxp command, as this is the export command that was also used throughout the rest of the syntax, which I "inherited".
The result is an Excel file with a single sheet named "&year." (not "14") and the data from the last macro call, year 14.

Does anybody know how this can be solved? Also, I would be glad for suggestions concerning a more elegant way than calling the macro 7 times. Like looping over a list of years or something.

   %macro MyMacro (year = );
    %local indsn;
    %let indsn=path&year..myfile1;
    
ods tagsets.excelxp File='P:\Folder\Folder\Folder\MyMacro.xls'  style=statistical options(sheet_name='&year.'); 

    proc tabulate data=&indsn;
    class var1 var2;
    table var1 all, var2 all/printmiss;
    title "var1 and var2, year: &year.";
    run;

ods tagsets.excelxp close;
    
    %mend MyMacro;
    
    %MyMacro(year=20)
    %MyMacro(year=19)
    %MyMacro(year=18)
    %MyMacro(year=17)
    %MyMacro(year=16)
    %MyMacro(year=15)
    %MyMacro(year=14)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛清羽墨安 2025-01-25 02:15:03

对于此类内容,您通常使用 BY 语句。

proc sort data=sashelp.cars out=work.cars;
  by origin;
run;

ods excel file="path/file.xlsx";

proc tabulate data=work.cars;
  by origin;
  class make type;
  table make all, type all / printmiss;
run;

ods excel close;

ODS 语句告诉 SAS ODS 输出的目的地。这取决于您的系统和许可证,您可以使用哪种 Excel 导出。我选择了 excel 而不是 excelxp,因为它生成 xlsx 而不是 xls

BY 语句将输出分组到不同的表中,并将输出写入不同的 Excel 工作表中。

另外,宏并不是真正需要的。如果输入数据集正确命名(例如basename_year),您可以使用将所有数据集读入一个数据集

data all_years;
  set basename_: indsname=dsn;
run;

,然后使用dsn字符串提取年份(如果不包含年份)在每个单独的数据集中。

For this kind of stuff, you usually use the BY statement.

proc sort data=sashelp.cars out=work.cars;
  by origin;
run;

ods excel file="path/file.xlsx";

proc tabulate data=work.cars;
  by origin;
  class make type;
  table make all, type all / printmiss;
run;

ods excel close;

The ODS statement tells SAS the destination for the ODS output. It depends on your system and license, which excel export you can use. I went for the excel instead of excelxp since it generates an xlsx instead of the xls.

The BY statement groups the output in different tables and also writes the output on different excel sheets.

Also, the macro is not really needed. If the input datasets are properly named (e. g. basename_year) you can read all of them into one dataset using

data all_years;
  set basename_: indsname=dsn;
run;

and then use the dsn string to extract the year if it is not contained in each individual dataset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文