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.

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

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