1

当链接到电子邮件发送的条件无法执行时,为什么这段代码仍然发送​​电子邮件?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

4 回答 4

3

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

请参阅以下内容:

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

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

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

于 2012-08-29T21:24:37.900 回答
2

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

控制电子邮件传递的一种方法是使用电子邮件“指令”而不是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 错误,因为原始语句中未提供“TO”地址FILENAME,但您可以忽略此错误。

于 2012-08-29T21:10:58.417 回答
1

如果没有更多背景,很难准确猜测您打算如何执行此操作,但我认为更简洁的方法是:

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

步骤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语句。

于 2012-08-30T04:37:46.303 回答
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;

跑;

于 2015-04-21T15:05:43.663 回答