The SAS Output Delivery System and reporting techniques

Send Email Only When Data Exists

Reply
Occasional Contributor
Posts: 7

Send Email Only When Data Exists

I have some code that sends an email from a proc print statement that works.  However, even if no data exists, the email is still being sent.  Is there a way to control this when sending using the proc print?  I'm sure there's a better/easier way to do this, but this is what I have now:

filename outfile email

to = (<emails>)

TYPE = 'TEXT/HTML'

subject = <re>

from = <from>

ods listing close;

ODS HTML Body = outfile style = sasweb;

title BOLD HEIGHT=4 <title>;

proc print data=<data> split='/' noobs;

  *PRINT FUNCTIONALITY HERE

run;

title;

ods html close;

Contributor
Posts: 43

Re: Send Email Only When Data Exists

I use SAS Macro code to decide if I should send an email.

First, set up a macro variable to check if your dataset has any observations in it.  (I am sure there are other ways to determine the number of observations in a dataset, so I'd like to hear input on that.)

%LET HaveData = 0 ;

data _null_ ;

  SET <yourdataset> nobs=NumObs ;

  CALL SYMPUT("HaveData", LEFT(NumObs)) ;

  stop ;

run ;

%PUT HaveData = &HaveData ;

Then, embed your email steps in a macro, so you can dynamically execute the code, if HaveData > 0, like:

%MACRO SendEmail ;

     %IF &HaveData > 0 %THEN %DO ;

          email code here...

     %END ;

%MEND SendEmail ;

* Execute the macro ;

%SendEmail

Occasional Contributor
Posts: 7

Re: Send Email Only When Data Exists

I completely understand where you're going with that and I think it would work perfectly.

My problem is that my data set ALWAYS has observations in it and i'm putting a where condition in my proc print to only display the records that I want.  I was curious if there was a way to send/not send emails using that method.

I do it this way because I frequently change my where clause to get different variations of the data.

Contributor
Posts: 43

Re: Send Email Only When Data Exists

I guess the simplest solution would be to add a data step to create a subset with your where clause, and then use THAT in your data _null_ step.

%LET HaveData = 0 ;

data yoursubset ;

     data yourdataset(where=(whereclause)) ;

run ;

data _null_ ;

  SET <yoursubset> nobs=NumObs ;

  CALL SYMPUT("HaveData", LEFT(NumObs)) ;

  stop ;

run ;

%PUT HaveData = &HaveData ;

Contributor
Posts: 43

Re: Send Email Only When Data Exists

Oh, just thought of another way to get the count, especially if you have a really large dataset, and you don't want to create a new subset.

proc sql noprint ;

     select count(*) into: HaveData

     from <yourdataset>

     where whereclause ;

quit ;

Occasional Contributor
Posts: 7

Re: Send Email Only When Data Exists

This worked perfectly.  Thanks!!!

Contributor
Posts: 21

Re: Send Email Only When Data Exists

Dear Akordula,

Please check the below code and see if it works for you.

data mydata;

  x = 1; output;

  x = 2; output;

  x = 3; output;

  x = 3; output;

run;

%macro sendmail;

  proc sql noprint;

  select count(*) into :n from mydata where x = 3;

  quit;

  %put &n;

  %if &n gt 0 %then

  %do;

  %put your email code here;

  %end;

  %else

  %do;

  %put Value are no values to print/mail;

  %end;

%mend;

%sendmail

Ask a Question
Discussion stats
  • 6 replies
  • 1124 views
  • 3 likes
  • 3 in conversation