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;
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
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.
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 ;
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 ;
This worked perfectly. Thanks!!!
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
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.