BookmarkSubscribeRSS Feed
akordula
Calcite | Level 5

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;

6 REPLIES 6
cwilson
Calcite | Level 5

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

akordula
Calcite | Level 5

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.

cwilson
Calcite | Level 5

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 ;

cwilson
Calcite | Level 5

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 ;

akordula
Calcite | Level 5

This worked perfectly.  Thanks!!!

AmitRathore
Obsidian | Level 7

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

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!

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
  • 6 replies
  • 5899 views
  • 3 likes
  • 3 in conversation