用于发送电子邮件的宏

时间:2016-01-21 06:36:13

标签: sas sas-macro

我的代码使用sas数据步骤发送邮件。这里试图创建一个发送邮件的代码。

FILENAME outbox EMAIL ("***********");
DATA _NULL_;
FILE outbox
TO=("************")
FROM=("***********")
SUBJECT=("Example of a SAS E-mail" );
/* ATTACH=(""); */
PUT " ";
PUT "Hello Boss,";
PUT " ";
PUT "Attached are the Daily Operational Reports.";
PUT " ";
PUT "rrt";
RUN;

2 个答案:

答案 0 :(得分:0)

我不倾向于在datastep本身中包含电子邮件参数,而是在fileref中。我已经用我的电子邮件地址测试了下面的代码,但它确实有效。

正如我在评论中所说,如果你想让它在宏中运行,你只需要将你的datastep包装在一个宏中。

您可以拥有位置或关键字参数。有关详细信息,请参阅此source。我在这个例子中使用了关键字参数。你通过指定关键字调用宏,然后像我显示最后一行那样调用值。如果你没有放任何东西,那么关键字会被忽略。

顺便说一下,空关键字会导致初始化的本地宏变量。让(%LET FROM = FROM ="& FROM")之类的语句可以添加字符串" FROM ="到" FROM"的开始宏变量,因此文件名语法将完整。如果宏变量如" FROM"将按原样传递,它将解析为该文件名的电子邮件地址,不知道该怎么做。

   %MACRO send_email (TO=,FROM=,SUBJECT=,ATTACHMENT=,BODY=);
    %IF &TO ne %THEN
        %LET TO="&TO";

    %IF &FROM ne %THEN
        %LET FROM=FROM="&FROM";

    %IF &SUBJECT ne %THEN
        %LET SUBJECT=SUBJECT="&SUBJECT";

    %IF &ATTACHMENT ne %THEN
        %LET ATTACHMENT=ATTACHMENT="&ATTACHMENT";

    %IF &BODY ne %THEN
        %LET BODY="&BODY";
    FILENAME outbox EMAIL &TO &FROM &SUBJECT &ATTACHMENT;

    DATA _NULL_;
        FILE outbox;
        PUT &BODY;
    RUN;

%MEND;

%send_email(email=example@example.com);

答案 1 :(得分:0)

Peeyush,

尝试下面的代码示例。确保输入您的SMTP电子邮件主机详细信息和电子邮件地址。如果您想要其他自动化,可以将电子邮件地址设为宏变量:

%macro emailM;
OPTIONS emailsys = SMTP emailhost = Youremail.Host.net emailport= 25;
FILENAME Mailbox EMAIL 'You@something.com';
attach=("\\somewhere\something\File.xls");
DATA _NULL_;
FILE MailBox TO=("somebody@something.com" "somebody2@something.com") 
Subject="Example of a SAS E-mail";
PUT "Hello Boss,";
PUT " ";
PUT "Attached are the Daily Operational Reports.";
PUT " ";
PUT "Thank you";
RUN;
%mend emailM;

%emailM;