BookmarkSubscribeRSS Feed
Eugenio211
Quartz | Level 8

Hi,

 

Hope all is well.

 

Does anybody have an email code where in if there is data an email will send with attachment and if there is no data just an email saying 'no data to report'?

 

Thank you in advance.

6 REPLIES 6
Kurt_Bremser
Super User

Please show your code which sends the mail when data is present.

Also describe what you mean by "no data". Dataset does not exist, or dataset has zero observations?

You can retrieve such information like this:

%let data = 0;

proc sql noprint;
select nobs into :data from dictionary.tables
where libnamem= "YOURLIB" and memname = "YOURDATASET";
/* library and dataset name must be uppercase */
quit;

You can then use &data in macro statements to run your code conditionally.

Eugenio211
Quartz | Level 8
hi below is the code I use to send email with attachement, I just want to add a step that if there is no data generated i'll send an email without attachment. thank you.

filename mymail email 'myemail@email.com' from='myemail@email.com'
subject='Daily Chargebacks Report'
attach=("\\shared\Auto Reports\Chargebacks Report.xlsx");

data _null_;
file mymail
to=("someone@emailcom
)
cc=("myeamil@email.com");
put 'Hi,';
put ' ';
put 'Attached is the Chargeback report.';
put ' ';
put 'Cheers';
run;
AhmedAl_Attar
Rhodochrosite | Level 12
IF you are using SAS 9.4 M5+, then you can add this to test for your data set

%if (%sysfunc(exists(lib.dataset) GT 0) %then
%do;
%end;
More details are listed in this blog
https://blogs.sas.com/content/sasdummy/2018/07/05/if-then-else-sas-programs/

Hope this helps
Kurt_Bremser
Super User

Use my previous code, then do

%if &data.
%then %do;

filename mymail email 'myemail@email.com' from='myemail@email.com'
subject='Daily Chargebacks Report'
attach=("\\shared\Auto Reports\Chargebacks Report.xlsx");

data _null_;
file mymail
to=("someone@emailcom
)
cc=("myeamil@email.com");
put 'Hi,';
put ' ';
put 'Attached is the Chargeback report.';
put ' ';
put 'Cheers';
run;

%end;
%else %do;

filename mymail email 'myemail@email.com' from='myemail@email.com'
subject='Daily Chargebacks Report'
;

data _null_;
file mymail
to=("someone@emailcom
)
cc=("myeamil@email.com");
put 'Hi,';
put ' ';
put 'No data was created!';
put ' ';
put 'Cheers';
run;

%end;
AhmedAl_Attar
Rhodochrosite | Level 12

Hi @Eugenio211 

Can you please clarify:

Did you want

  • SAS code to send the email

OR

  •  OS script (Windows: PowerShell/Bat | Linux/Unix: Shell Script)

Which one? 

The syntax would vary depending on your choice of language.

Eugenio211
Quartz | Level 8
SAS code to send the email, above is the
code I use to send the email. thank you.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 599 views
  • 0 likes
  • 3 in conversation