Christian Stanyer
Christian Stanyer

Reputation: 65

Sending an email from SAS only if certain conditions are met

I want SAS to send an email out, but only if a global macro variable &warning is equal to 1.

Is this possible? I'm trying the following, but it's not working. It still sends an email when warning=0.

filename outbox email
               to=('[email protected]')
               subject='Warning Report'
               from='[email protected]'
               attach='/report.html';

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

Upvotes: 1

Views: 2144

Answers (4)

Richard
Richard

Reputation: 27508

You can not conditionally run a step based on a if expression within the step.

You could continue to have an open-code STEP and conditionally modify the RUN statement to become RUN CANCEL with a judicious use of %sysfunc(ifc. The 'benefit' being you don't need to embed the logic within a separate macro.

%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,));

Upvotes: 0

Tom
Tom

Reputation: 51611

You should be able to use email directives to abort the message.

!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;

Upvotes: 5

Wired604
Wired604

Reputation: 370

I think it's because you don't use then, even thereI think there will be a syntax issue and SAS will not be able to finish that code block or return an error.... You can put it in a macro and it will work.

try something like this

%macro email(condition=);

%if &condition.=1 %then %do;
filename outbox email
               to=('[email protected]')
               subject='Warning Report'
               from='[email protected]'
               attach='/report.html';

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

Upvotes: 1

John Doe
John Doe

Reputation: 676

Try this:

  %let warning=1;

  %macro send();
  %if &warning. =1 %then %do;
   filename outbox email
           to=('[email protected]')
           subject='Warning Report'
           from='[email protected]'
           ;

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

  %send;

Upvotes: 1

Related Questions