BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Rebecca_K
Fluorite | Level 6

Hello!  I'd like to write the contents of a table to an email, any ideas how to go about this? 

Here's what I have so far.  In the section that reads "PUT RESULTS HERE" I'd like to write:

Member: 12345678901   Amount: 10
Member: 1234567892    Amount: 6
Member: 1234567893 Amount: 5

etc.

DATA WANT;
INPUT MEMBER : $20. AMOUNT : 8. ;
CARDS;
1234567891 10 
1234567892 6
1234567893 5
1122334455 4
;
run;
filename outmail email 
	to=&toEmail. 
	from=&fromEmail. 

	subject="Results are in";

	data _null_;
	file outmail;
	put Here are the results:  ;
/* PUT RESULTS HERE */

run;

Thanks in advance!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Are you sending one email with all 6 of those data points?

filename out email ....... ;
data _null_;
  file out;
  set want ;
  put member= amount=;
run;

Or are you sending separate emails to each of the members? In which case where are you going to find the email address of where to send it?  If you have the address in the dataset then just use EMAIL DIRECTIVES in the data step.

 

Let's adapt their example to your example. So try something like this:

data have;
  input MEMBER :$20. AMOUNT EMAIL $40. ;
CARDS;
1234567891 10 team1@x.com
1234567892 6  team2@y.com
1234567893 5  team3@z.com
1122334455 4  team3@z.com
;

filename reports email 'Member amount';

data _null_;
  file reports;
  set have ;
  by email ;
  if first.email then do; 
* Set email attributes and write header of report ;
    put '!EM_NEWMSG!';                    
    put '!EM_TO!' email;
    put '!EM_SUBJECT! Report for ' email;  
    put 'Here is the latest report for ' email '.';
  end;
* send the data message ;
    put member= amount= ;

  if last.email then do;
* send the message ;
    put '!EM_SEND!';                      
  end;
run; 

 

 

 

 

View solution in original post

8 REPLIES 8
SASJedi
Ammonite | Level 13

Have a look at Tips and Tricks for Sending Emails Using SAS: Q&A, Slides, and On-Demand Webinar - there are several great options and examples provided there. 

 

 

Check out my Jedi SAS Tricks for SAS Users
Rebecca_K
Fluorite | Level 6

Hi, thanks for the reply.   I skimmed through the hour long webinar you suggested, and see that you can write a proc report to an email.  I tried to keep it basic to start.  Email was sent, but the data from the table did not write.  Am I missing something?  Here is the code:

filename outmail email 
	to=&toEmail. 
	from=&fromEmail. 

	subject="Results are in";

	data _null_;
	file outmail;
	put "Here are the results:  ";

/* NEW CODE HERE: */
   proc Report data=Want;
   run;


run;
SASJedi
Ammonite | Level 13

Yes - see pages 10 and 11 of the handout (PDF). The idea is to use ODS to capture the PROC output and write it to your email file:

filename mail email ... ;
ods html(id=email) file=mail style=daisy;
/* PROC output here */
ods html(id=email) close;
Check out my Jedi SAS Tricks for SAS Users
Rebecca_K
Fluorite | Level 6

Thanks for the tip!  Question:  I am still fairly new, when you said there's a PDF file, how do i find that?  I went to the Webinar you linked, but I'm not sure where i can find the file.  Much appreciate your help!

Rebecca_K_0-1729196533724.png

 

Tom
Super User Tom
Super User

Are you sending one email with all 6 of those data points?

filename out email ....... ;
data _null_;
  file out;
  set want ;
  put member= amount=;
run;

Or are you sending separate emails to each of the members? In which case where are you going to find the email address of where to send it?  If you have the address in the dataset then just use EMAIL DIRECTIVES in the data step.

 

Let's adapt their example to your example. So try something like this:

data have;
  input MEMBER :$20. AMOUNT EMAIL $40. ;
CARDS;
1234567891 10 team1@x.com
1234567892 6  team2@y.com
1234567893 5  team3@z.com
1122334455 4  team3@z.com
;

filename reports email 'Member amount';

data _null_;
  file reports;
  set have ;
  by email ;
  if first.email then do; 
* Set email attributes and write header of report ;
    put '!EM_NEWMSG!';                    
    put '!EM_TO!' email;
    put '!EM_SUBJECT! Report for ' email;  
    put 'Here is the latest report for ' email '.';
  end;
* send the data message ;
    put member= amount= ;

  if last.email then do;
* send the message ;
    put '!EM_SEND!';                      
  end;
run; 

 

 

 

 

Rebecca_K
Fluorite | Level 6

Thanks for the feedback Tom!  The first example is what I'm going for, with all results in a single email.  I'm trying your suggestion, but can't figure out how to write a line of text, then dump the table outputs.  It wants to write the line of text X number of times, to go with the table results.  thoughts?  Here's my code, and below it is the result (with "Here are your results" repeated).  Thanks again!!

%macro runemail;
	filename outmail email 
		to=&toEmail. 
		from=&fromEmail. ;

	data _null_;
	file outmail;
	put "Here are your results:";
	put;

     set want;
  	 put Member= Amount=;

	run;

%mend runemail;
Here are your results:

MEMBER=1234567891 AMOUNT=10
Here are your results:

MEMBER=1234567892 AMOUNT=6
Here are your results:

MEMBER=1234567893 AMOUNT=5
Here are your results:

MEMBER=1122334455 AMOUNT=4
Here are your results:
Tom
Super User Tom
Super User

Only write the static text the FIRST time the data step executes.

if _n_=1 then put ....
Rebecca_K
Fluorite | Level 6

Perfect!  Thanks for your help! 

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 8 replies
  • 1814 views
  • 0 likes
  • 3 in conversation