BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jerathashutosh
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

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;
jerathashutosh
Fluorite | Level 6

works like a charm!

 

Thanks Tom.

 

Ashu

ChrisNZ
Tourmaline | Level 20

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;

 

 

jerathashutosh
Fluorite | Level 6

this works perfectly as well! 😄

 

Thanks ChrisNZ.

 

Ashu

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2815 views
  • 2 likes
  • 3 in conversation