从SAS程序发送电子邮件

时间:2012-08-29 20:16:11

标签: sas

为什么此代码在链接到电子邮件的条件时仍会发送电子邮件     发送不能执行? SASLOG证明条件值为零,x =。

98   filename outbox email 'dan.xxxxx@zz.com';
99
100  data _null_;
    /* Set variable */
101     x=0;  
102     put x=;
    /* condition cannot be true */
103     if x=1 then do;  
104        link send_email;  /* should only be hit if condition is true */
105     end;
106  return;
107
108  send_email:  /* seems to get executed no matter what */
109     file outbox
110     to=("dan.xxxxx@zzcom")
111     subject="Email test";
112     put "Email test from SAS program";
113     put " ";
114  return;
115  run;

NOTE: The file OUTBOX is:
      E-Mail Access Device

x=0
Message sent
      To:          ("dan.xxxxx@zz.com" )
      Cc:
      Bcc:
      Subject:     Email test
      Attachments:
NOTE: 0 records were written to the file OUTBOX.
NOTE: DATA statement used (Total process time):
      real time           7.22 seconds
      cpu time            0.35 seconds

4 个答案:

答案 0 :(得分:3)

FILE不是条件可执行语句,而是编译时语句。即使没有记录输出到文件,仍然会创建文件,在这种情况下,即使没有记录将数据放入其中,大概仍然会发送电子邮件。

请参阅以下内容:

data _null_;
set sashelp.class;
if 0 then do;
    file "c:\temp\temp.txt";
    put age;
end;
run;

即使没有记录放入文件,您仍然会在日志中看到有关正在创建的文件的信息。

如果您尝试有条件地发送电子邮件或不发送电子邮件,您可能希望返回有关您正在做的事情的更多信息,以便获得有关如何执行此操作的有用回复。

答案 1 :(得分:2)

在看到Joe更正确的答案之后,我完全重写了我的。

您可以控制电子邮件递送的一种方法是使用电子邮件“指令”而不是FILE语句中的选项。例如:

filename outbox email;
data _null_;
   x=0;  
   put x=;
   if x=1 then do;  
      link send_email;
      end;
   return;
send_email:  
   file outbox;
   put '!em_to! ( "dan.xxxxx@zzcom" )';
   put '!em_subject! Email Test';

   put "Email test from SAS program";
   put " ";
return;
run;
filename outbox clear;

现在,当您执行此操作时,您将收到SAS错误,因为原始FILENAME语句中未提供“TO”地址,但这是您可以忽略的错误。

答案 2 :(得分:1)

如果没有更多背景知识,很难准确猜出你打算如何做到这一点,但我认为更清洁的方法是:

  1. 确定是否应发送电子邮件。
  2. 使用宏代码中的datastep有条件地发送电子邮件。
  3. 第1步:

    %let send_email = 0;  * WHEN 0 EMAIL WILL NOT BE SENT. WHEN 1 EMAIL WILL BE SENT;
    
    data _null_;
      set mydata;
      if x = 1 then do;
        call symput('send_email',1);
        stop; * LEAVE THE DATASTEP AS SOON AS WE DECIDE AN EMAIL SHOULD BE SENT;
      end;
    run;
    

    第2步:

    %macro send_email;
      %if &send_email eq 1 %then %do;
        filename outbox email 'dan.xxxxx@zz.com';
        data _null_;
          file outbox to=("dan.xxxxx@zzcom") subject="Email test";
          put "Email test from SAS program";
          put " ";
        run;
      %end;
    %mend;
    %send_email;
    

    编辑:将代码中的return语句更改为stop语句。

答案 3 :(得分:0)

*Just modified Step 1 to not send emails on weekends - Works great!

%let send_email = 0;  * WHEN 0 EMAIL WILL NOT BE SENT. WHEN 1 EMAIL WILL BE SENT;

data _null_;
   dt=today();
   weekday=weekday(dt);
   if weekday ne 7 or weekday ne 1 then do;
     call symput('send_email',1);
     stop; * LEAVE THE DATASTEP AS SOON AS WE DECIDE AN EMAIL SHOULD BE  SENT;
   end;

运行;