BookmarkSubscribeRSS Feed
SIgnificatif
Quartz | Level 8

Hi, I would like to ask you how to conditionally send an email from sas ? 

 

 

%let variable1 ='go';

 if variable1 = 'go' then do;


FILENAME mail
EMAIL TO=(&emails)
etc ....
end;

Thank you.

 

 

6 REPLIES 6
Jagadishkatam
Amethyst | Level 16

You can something like below, consider that the code is within the macro sample, this is an untested code

 

%macro sample;

%global variable1;

%let variable1=go;

%if &variable1=go %then %do;
filename mail email to='xxxxx@xxxx.com';
data _null_;
file mail;
put 'Hi';
run;
%end;

%mend sample;

%sample
Thanks,
Jag
Kurt_Bremser
Super User

Starting with SAS 9.4M5 (I think), %if can be used in open code (no macro definition necessary):

%let variable1=go;

%if &variable1=go %then %do;


FILENAME mail
EMAIL TO=(&emails)
etc ....

%end;

Note that macro variables very rarely need quotes, quote are usually unnecessary and cause problems.

Tom
Super User Tom
Super User

Is VARIABLE a macro variable, as it it appears in your first statement, or an actual variable in a SAS dataset, as it appears in your second statement?  If the later does the data have just one observation? If not what if some of the observations have values of go and other observations have values that are something else? Do you still want to send the email?

SIgnificatif
Quartz | Level 8
it's a macro variable
1 I need to use a condition to check the value ,
2 else send another email. ( i found that there is no possibility to do an elseif if in macro variable mode) , limiting this to only 2 conditions...

Tom
Super User Tom
Super User

Not sure what an elseif is, but there is definitely a macro %ELSE statement.

 

%if &variable=go %then %do;
....
%end;
%else %if &variable=stop %then %do;
....
%end;
%else %do;
....
%end;
Kurt_Bremser
Super User

@SIgnificatif wrote:
it's a macro variable
1 I need to use a condition to check the value ,
2 else send another email. ( i found that there is no possibility to do an elseif if in macro variable mode) , limiting this to only 2 conditions...


You can use %else %if, as @Tom already showed, but this needs a macro definition. To implement the same logic in open code, you need to code the else-path with a separate %if:

%if &variable1 = go
%then %do;

%end;
%if &variable1 ne go
%then %do;

%end;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 3541 views
  • 0 likes
  • 4 in conversation