Hi,
I have a daily email that goes out, but i want to send the email only when data exists. Currently it sends an email no matter if the data exists or not.
Currently i make a dataset with the desired data and then i run this code to send an email:
filename outbox email
to=("abc@gmail.com")
type='text/html'
subject="Today's Details"
from='abc@gmail.com';
ods html body=outbox;
proc print data= WORK.EMAIL;
run;
ods html close;
is there a way i can do something like:
if count is not null then (
filename outbox email
to=("abc@gmail.com")
type='text/html'
subject="Today's Details"
from='abc@gmail.com';
ods html body=outbox;
proc print data= WORK.EMAIL;
run;
ods html close;
)
else exit;
Thanks.
Ashu
You can use macro code to generate SAS code. With latest version of SAS you can even using simple %IF/%THEN/%DO statements in a regular SAS program. For older versions (or more complex macro logic) you will need to wrap the code into a macro definition.
data _null_;
call symputx('nobs',nobs);
stop;
set work.email nobs=nobs;
run;
%if (&nobs) %then %do;
filename outbox email
to=("abc@gmail.com")
type='text/html'
subject="Today's Details"
from='abc@gmail.com'
;
ods html body=outbox;
proc print data= WORK.EMAIL;
run;
ods html close;
%end;
You can use macro code to generate SAS code. With latest version of SAS you can even using simple %IF/%THEN/%DO statements in a regular SAS program. For older versions (or more complex macro logic) you will need to wrap the code into a macro definition.
data _null_;
call symputx('nobs',nobs);
stop;
set work.email nobs=nobs;
run;
%if (&nobs) %then %do;
filename outbox email
to=("abc@gmail.com")
type='text/html'
subject="Today's Details"
from='abc@gmail.com'
;
ods html body=outbox;
proc print data= WORK.EMAIL;
run;
ods html close;
%end;
works like a charm!
Thanks Tom.
Ashu
Or, if you want to avoid writing macros:
data _null_;
if NOBS then call execute(catt(
"filename OUTBOX email "
, " to=('abc@gmail.com') "
, " type='text/html' "
, " subject=""Today's Details"" "
, " from='abc@gmail.com' ; "
, "ods html body=OUTBOX; "
, "proc print data=WORK.EMAIL; run; "
, "ods html close; "
, "filename OUTBOX clear; " ));
stop;
set WORK.EMAIL nobs=NOBS;
run;
this works perfectly as well! 😄
Thanks ChrisNZ.
Ashu
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.