0

我一直在关注在线文档并浏览其他堆栈溢出查询,但我还没有找到一种通过 DDE 将我的 SAS 数据集输出到 excel 的方法。

我运行的SAS版本是SAS9.4 我运行的excel版本是microsoft office 2016 - excel 2016

我用来导出的代码是

/*Excel DDE interface options*/ /*TEST*/
options noxwait noxsync;

X '"C:\Users\user.name\Desktop\template_dde.xlsx"';

data _null_;
    rc=sleep(15);
run;

filename ddedata dde 'excel|SFA!r2c1:r4000c56';

data _null_;
    file ddedata notab;
    set work.Results_output_format end=eof;
put '"THIS IS A TEST"';
run;

%LET timestamp = %SYSFUNC(PUTN(%SYSFUNC(DATE()),yymmddn8.));
%LET hourstamp = %SYSFUNC(COMPRESS(%SYSFUNC(TIME(),time.),%STR( :)));

data _null_;
    length cmnd $150.;
    file ddedata;

    cmnd = '"[save.as("C:\Users\user.name\Desktop\&timestamp._&hourstamp._template_dde.xlsx")]"';
    put cmnd;
    put '[quit()]';
run;

它输出“这是一个测试”,然后输出保存语句,但我的数据没有导出,文件也没有实际保存。

我忽略了什么吗?

4

1 回答 1

2

SET 不会数据集内容隐式放置在 Excel 文件中。您需要使用PUT语句将数据添加到工作表中。您还需要使用单独的文件名向Excel|system通道发送命令。

您的代码在尝试的宏变量解析周围有单引号 - 这是不正确的。

此代码假定存在现有工作簿c:\temp\template_dde.xlsx

* open template in Excel;
X '"C:\Temp\template_dde.xlsx"';

* wait for app to start and file to load;
data _null_; rc=sleep(3); run;

* define filerefs for data transfer and command execution;
filename ddedata dde 'excel|Sheet1!r2c1:r4000c56';
filename ddecmnd dde 'excel|System';

* pump data into excel cells at location specified in ddedata;
data _null_;
  file ddedata ; * <--- removed your NOTAB option, so now I dont have to put '09x' between each variable;

  set sashelp.class;
  put name sex age height weight; 
run;

* Extended ISO timestamp as yyyy-mm-dd_hh-mm-ss;
%let timestamp = %sysfunc(translate(%sysfunc(datetime(),E8601DT),%str(_-),%str(T:)));

* send commands to save workbook and close Excel;
data _null_;
    file ddecmnd;

    put "[save.as(""C:\Temp\&timestamp._template_dde.xlsx"")]";
    put '[quit()]';
run;

大量关于 DDE 的会议论文,例如“SAS®-with-Excel Application Development: Tools and Techniques”、SUGI 31、LeRoy Bessler、Assurant Health

于 2018-10-31T11:42:11.393 回答