In your organization, are you prevented from using the email alias of a distribution list (e.g. groupname@comp.domain.com), and would therefore like to extract the list recipients into a flat file to loop through/use the SAS Enterprise Miner functions, etc. to send the mail?
This question was recently asked by a student in our SAS Programming 3: Advanced Techniques and Efficiencies course. Instructor Christine Riddiough shares some sample code she’s used to solve this challenge. This will work for a distribution list in an Exchange Server running on UNIX. The OPTIONS statement would need to be adjusted for a different platform or email server.
/* macro to email a file to a list */ %macro EmailFile; /* This creates a series of macro variables to send email to individuals. */ proc sort data=freqstart out=list(keep=email) nodupkey; by inst; run; data _null_; set list end=last; call symput(compress('email'||_n_),email); if last then call symput('tot',trim(left(_n_))); run; /* cycle through the individual emails to send the messages */ %do i=1 %to &tot; /* if you want to attach a file use the filename statement and the pathname function to capture the location of the file */ filename freq "&fn.crr.csv"; %let path=%sysfunc(pathname(freq)); ods csv file=freq; proc print data=freqstart noobs; run; ods csv close; /* set options for email and use the filename statement with the email option to send email */ options emailsys=smtp emailhost=mailhost.abc.xyz.com; filename mymail email "&&email&i" subject="Frequency Template for &start to &end" attach="&path"; /* Add some text to the email */ data _null_; file mymail; put 'Attached is a report.' /; put "Return the modified report to Jane.Smith@xyz.com by &date.."; run; %end; %mend; %EmailFile |
3 Comments
This is for SAS. Is it also for JMP? if not, do you have the version that is usable from JMP that will Email a file to list of Emails in 1 swoop?
Can I use Google mail as the mean for the Email?
Please let me know.
Thanks,
1) Maybe I'm missing something here, but it seems to me there should by a %end statement somewhere to close the %do loop, but I can't find one.
2) Instead of sending out n e-mails, each addressed to a single party, would it not be more efficient to send one e-mail addresses to n parties. This is relatively easy to do by constructing a list of recipients in the TO, CC, or BCC arguments of the e-mail, and is supported by the SAS e-mail processor.
Cheers
Hi Peter,
Thanks for your comment! I spoke with Christine and here is her response:
1) Yes there should be a %end; right before the %mend; -- I deleted it by mistake when I edited the file for the blog -- good catch.
2) Yes you can do that. I haven't tested the efficiency, but it would avoid some of the data steps. Which way you use might depend on the specifics of the situation.
Thanks,
Michele