仅在满足特定条件时从 SAS 发送电子邮件

Sending an email from SAS only if certain conditions are met

我希望 SAS 发送电子邮件,但前提是全局宏变量 &warning 等于 1。

这可能吗?我正在尝试以下操作,但它不起作用。当 warning=0 时它仍然发送电子邮件。

filename outbox email
               to=('me@myemail.com')
               subject='Warning Report'
               from='you@myemail.com'
               attach='/report.html';

DATA _null_;
file outbox;
Put "Hello,"//
"Warning report attached."//
"Regards,"/
"Chris";
if &warning. =1
run;

试试这个:

  %let warning=1;

  %macro send();
  %if &warning. =1 %then %do;
   filename outbox email
           to=('myemail@mail.com')
           subject='Warning Report'
           from='you@myemail.com'
           ;

  DATA _null_;
  file outbox;
  Put "Hello,"//
  "Warning report attached."//
  "Regards,"/
  "Chris";
  run;
  %end;
  %mend;

  %send;

我认为这是因为你不使用 然后 ,即使在那里我认为会有语法问题并且 SAS 将无法完成该代码块或 return 一个错误.... 你可以把它放在一个宏中,它会起作用。

尝试这样的事情

%macro email(condition=);

%if &condition.=1 %then %do;
filename outbox email
               to=('me@myemail.com')
               subject='Warning Report'
               from='you@myemail.com'
               attach='/report.html';

DATA _null_;
file outbox;
Put "Hello,";
Put "Warning report attached.";
Put "Regards,";
Put "Chris";
run;
%end;
%mend;
%email(condition=&warning.);

您应该能够使用电子邮件指令中止邮件。

!EM_ABORT! stops the current message. You can use this directive to stop SAS software from automatically sending the message at the end of the DATA step.

data _null_;
  file outbox;
  if &warning. then do;
    put "Hello,"
     // "Warning report attached."
     // "Regards,"
      / "Chris"
    ;
  end;
  else put '!EM_ABORT!';
run;

您不能有条件地 运行 基于步骤中的 if 表达式的步骤。

您可以继续使用开放代码 STEP,并有条件地将 RUN 语句修改为 RUN CANCEL,并明智地使用 %sysfunc(ifc。 'benefit' 因为您不需要将逻辑嵌入单独的宏中。

%let warning = 0;
data _null_;
  put "NOTE: macro symbol 'warning' is &warning";
run %sysfunc(ifc(&warning,CANCEL,));

%let warning = 1;
data _null_;
  %* never executed because step is cancelled;
  put "NOTE: macro symbol 'warning' is &warning";
run %sysfunc(ifc(&warning,CANCEL,));