SAS 宏:将表导出到一个 Excel 文件中的不同工作表
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于此类内容,您通常使用
BY
语句。ODS
语句告诉 SAS ODS 输出的目的地。这取决于您的系统和许可证,您可以使用哪种 Excel 导出。我选择了excel
而不是excelxp
,因为它生成xlsx
而不是xls
。BY
语句将输出分组到不同的表中,并将输出写入不同的 Excel 工作表中。另外,宏并不是真正需要的。如果输入数据集正确命名(例如
basename_year
),您可以使用将所有数据集读入一个数据集,然后使用
dsn
字符串提取年份(如果不包含年份)在每个单独的数据集中。For this kind of stuff, you usually use the
BY
statement.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 theexcel
instead ofexcelxp
since it generates anxlsx
instead of thexls
.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 usingand then use the
dsn
string to extract the year if it is not contained in each individual dataset.