SAS:Proc导出和电子邮件错误?

时间:2017-07-18 13:35:41

标签: sql email sas export

我每天都有代码运行。目前,它正在创建一个Excel文件(使用proc导出,见下文),它将在本地导出到文件路径。

proc export data=work.data_set
outfile="FILE LOCATION AND NAME HERE" 
dbms=xlsx
replace;
sheet="Data sheet";
run;

现在我想更改它,以便它仍然会生成excel文件,但不是在本地存储它,我希望通过电子邮件发送它。这该怎么做?我试着这样做:

proc export data=work.data_set
  outfile= "FILE LOCATION AND NAME HERE"
  dbms=xlsx
  replace;
  sheet="Data sheet";
run;

        FILENAME demail EMAIL 
                        TO = ("xxx@xxx.com")
                        SUBJECT ="Data sheet"
                        FROM = "xxx@xxx.com" 
                        ATTACH = (  "FILE LOCATION AND NAME HERE");
        DATA _NULL_;
        FILE demail;
        nu = PUT(TODAY(),DDMMYYD10.);
        PUT 'Hello,';
        PUT;
        PUT 'Bye.'; 
        PUT;
        RUN;

但它给了我这个错误: “Excel在XXXX.xlsx中找到了不可读的内容。是否要恢复此工作箱的内容?如果您信任此工作簿的来源,请单击是。” 单击是没有帮助。

谢谢!

2 个答案:

答案 0 :(得分:1)

从代码中删除此行:ATTACH = ( "FILE LOCATION AND NAME HERE");

将这两行添加到您的代码中:

%let filepath= FILE LOCATION AND NAME HERE without quotes; 
put "!EM_ATTACH! &filepath"; 

完整代码:

%let filepath= FILE LOCATION AND NAME HERE without quotes; 
proc export data=work.data_set
  outfile= "FILE LOCATION AND NAME HERE"
  dbms=xlsx
  replace;
  sheet="Data sheet";
run;

        FILENAME demail EMAIL 
                        TO = ("xxx@xxx.com")
                        SUBJECT ="Data sheet"
                        FROM = "xxx@xxx.com" 
                        ;
        DATA _NULL_;
        FILE demail;
        nu = PUT(TODAY(),DDMMYYD10.);
        PUT 'Hello,';
        put "!EM_ATTACH! &filepath"; 
        PUT;
        PUT 'Bye.'; 
        PUT;
        RUN;

答案 1 :(得分:0)

除了filename email

之外,您的脚本中的所有内容都是正确的

SAS不知道xlsx文件的content_type,因为默认情况下它将所有内容都视为二进制文本文件。因此,弄乱你的excel文件的完整性。因此,您必须明确提及您的文件content_type。所以,只需添加filename email声明 -

FILENAME demail EMAIL 
TO = ("xxx@xxx.com")
SUBJECT ="Data sheet"
FROM = "xxx@xxx.com" 
ATTACH = ("%sysfunc(pathname(xllib))/my_excel_file.xlsx" ct="application/xlsx") /*check for the ct, abbr for content_type*/
ct = "text/html";

然后发送一封包含data _null_;

的电子邮件
data _null_;
file demail;
put "Hello";
put "<br><br>"; /*two html style breaks, because it supports html tags for inserting a blank line. Simply put would not insert a line I guess*/
put "Bye!";
run;