Wednesday, March 31, 2010

How to Manage Emails from SAS

Teo Gamishev, Office of Research and Statistics, Columbia, SC

Download the PDF version from here

ABSTRACT
Setting up part of your email workload on autopilot from within SAS can eliminate the need of redundant work and significantly increase your job efficiency when dealing with repetitive tasks. SAS has built in emails generating engine, which when tuned properly, works very well with every email provider. Here you’ll find an example of fully automated process of emailing reports to several customers, using SAS.

INTRODUCTION
If you are emailing reports to your customers on a regular basis, then you definitely can benefit from the material laid out in this article. At least, it will motivate you to start testing, tweaking and having fun reading your first, successfully delivered emails. At the end you may end up using and enjoying this fully automated emailing system.

PROBLEMS
Here is a list of the problems covered on the following pages:
How to tune up your program to successfully deliver emails to a list of customers and supervisors;
How to generate the emails;
How to automate the entire process;

SOLUTIONS
TUNING UP SAS EMAIL ENGINE is not a rocket science, however I found it was not easy to locate the proper instructions on that. So, here is how it can be done:

EXAMPLE:
OPTIONS EMAILAUTHPROTOCOL=NONE
EMAILHOST=”mail.my.state”
EMAILID=”My.Name@MyCompany.com”
EMAILSYS=SMTP;

Email providers should be accessible from within the company without necessity of logging in and using a password. If that is not possible, then you should change the
EMAILAUTHPROTOCOL=LOGIN

After failing many times when trying to connect to my email provider, using the login protocol, our IT person gave me the hint of using a different host name to connect to it, without using user ID and password. You need to do your research and type the right hostname under EMAILHOST option, in order to assure smooth operation.

The EMAILID option contains your email address. If you just type a name or invalid email that may cause the email providers to filter out your messages or force them into the junk box. On another side, our email provider recognizes the fact that the sender and one of the recipients under the CC list are the same and refuses to deliver a copy of the emails to my Inbox. Your email provider may not be so smart, but you can always be creative here and use another email of yours to email a copy to yourself, if so needed.

The EMAILSYS option should be adjusted to SMTP(Simple Mail Transfer Protocol). The other possibilities are: MAPI(Messaging Application Program Interface) or VIM(Vendor Independent Messaging) . Those two may require login and password to access the server.

Here is how you tune your program for that:
OPTIONS EMAILAUTHPROTOCOL=LOGIN
EMAILHOST=”ciomail.my.state”
EMAILID="MyLogin" /* your email login */
EMAILPW="MyPass" /* your email password */
EMAILSYS=MAPI;

The fact that I have to type and leave sensitive information in my program doesn’t make me feel good about that. My advice is to move that entire OPTIONS statement into your AUTOEXEC.SAS file. However, make sure that your profile is in use every time when open a new SAS session. Not all servers, where SAS has been installed, are set up that way. Most of them will read the information in your AUTOEXEC.SAS during the first session and will skip it for the next sessions. Read the message into your log right after opening the second session. If you see the following message posted there, you will know that the OPTIONS statement and all other statements in the AUTOEXEC.SAS has not been activated. Copy and submit them from within your session, first.

NOTE: Unable to open SASUSER.PROFILE. WORK.PROFILE will be opened instead.
NOTE: All profile changes will be lost at the end of the session.

Once all of the above tuning is done, it is a good idea to check the options by submitting the following code:
proc options group=email; run;

GENERATING THE EMAILS from within SAS is an easy task to do. Let’s take a look at the example below:
EXAMPLE:
FILENAME outbox EMAIL
TO=”Perfect.Client@agency.gov”
CC=”Smart.Manager@company.com”
SUBJECT="Monthly Reports Uploaded"
;

data _null_;
file outbox;
put "Good Morning, ";
put " ";
put "The monthly Reports are uploaded";
put "into your Secure FTP folder.";
put " ";
put "Let me know if you have any questions. ";
put "Thank you and have a nice day";
put " ";
put "My Name";
put "ph:898-9999";
put "My.Email@company.com";
run;
There are two ways to address the recipient of the email:
By using the TO= option
FILENAME outbox EMAIL
TO=”Perfect.Client@agency.gov”
or
TO="My Customer "

Or without using the TO= option and listing the recipient as an argument right after the EMAIL device type in the FILENAME statement:
FILENAME outbox EMAIL ”Perfect.Client@agency.gov”

Listing multiple recipients can be done by using the TO= option:
FILENAME outbox EMAIL
TO= (”Perfect.Client@agency.gov”
”Dream.Client@agency.gov”
”Polite.Client@agency.gov”)
In a similar way, multiple recipients can be listed under CC=:
CC= (”Best.Supervisor@company.gov”
”Smart.Manager@company.gov”
”Awesome.Boss@company.gov”)
Other useful options are:
FROM= /* your email */
BCC= /* recipient(s) that you want to receive a blind copy */
REPLYTO= /* who will receive replies */
SUBJECT= /* subject of the message */
ATTACH= /* physical name of the file(s) to be attached */

Make sure to enclose the SUBJECT= into double quotes, If the subject contains special characters or more than one word (that is, it contains at least one blank space).
EXAMPLE:
SUBJECT=Sales
or
SUBJECT="June Sales Report"
Note: If you do not enclose a one-word subject in quotation marks, it is converted to uppercase.
One or more files can be attached to the email:
EXAMPLE:
ATTACH=u/myfiles/report.txt"
or
ATTACH=('D:\Reports\June2001.txt' 'C:\Reports\July2001.txt')
or
ATTACH="user.misc.pds(member)"

AUTOMATING THE ENTIRE PROCESS can give you the flexibility to modify and the power to send the email(s) with a single click. All the variables can be controlled within a one program (driving program) and the execution of the emailing can be done from another program (work program). Below is an example on how that can be achieved:
1. The following code is located in the DRIVING program:

**********************************************************************;
*** STEP X ***;
*** EMAILING TO MY CUSTOMERS ***;
**********************************************************************;

*** PLEASE CHANGE THE DATA BELOW, AS NECESSARY;

*** reduces the hustle, when the programs are moved to a new server***;
%let thepath=D:\Projects\FS;

%let myemail=My.Name@MyCompany.com;
%let myname=My Name;
%let myphone=(803)888-7777;

%let clients=”Perfect.Client@agency.gov”
”Dream.Client@agency.gov”
”Polite.Client@agency.gov”;
%let CCline=”Best.Supervisor@company.gov”
”Smart.Manager@company.gov”
”Awesome.Boss@company.gov”;

*** The program below emails the notifying message to all clients;
%include "&thepath\Programs\EmailsFS.sas";

*** SELECT and SUBMIT the above step;

**********************************************************************;
*** END OF STEP X ***;
**********************************************************************;
The driving program manages the values of all variables as the email sender’s data, clients’ data by storing them into macro variables.
We do not recommend emailing sensible data using emails and that’s why the attachment option is omitted here.
Once all those variables are handled, the execution is done by the work program EmailsFS.sas.
Here is how it may look:
***************************************************************************;
*** TUNING THE EMAIL OPTIONS and EMAIL DEVICE ***;
*** CREATING THE MESSAGE and EMAILING TO ALL CLIENTS ***;
***************************************************************************;

*** check all clients and other recipients are properly recorded ***;
%put _user_;

*** setting up the EMAIL options ***;
options
EMAILHOST="mymail.server.com"
EMAILID="&myemail"
EMAILSYS=SMTP;

*** check the log for all options being properly set up ***;
proc options group=email;run;

*** tuning the EMAIL device options ***;
FILENAME outbox EMAIL
TO=(&clients)
CC=(&CCline)
SUBJECT="Monthly CHIP Reports Uploaded"
;

data _null_;
file outbox;
put "The monthly Reports are uploaded";
put "into your Secure FTP folder.";
put "&myname";
put "&myphone";
put "&myemail";
run;
*** EOF ***;
All of the macro variables are plugged into the proper options and that assures correct and immediate execution.
Once the values are correctly stored into the driving program, all of the work is reduced to a single click of the mouse.

CONCLUSION
This paper offers one of the many possible solutions on how to use the SAS EMAIL device. It can be very practical and time saving to implement it in the daily routine. Automation of the tedious work tasks is always fun and motivates for more creativity at work.
SAS Global Forum 2007 Coders’ Corner

REFFERENCES
SAS Institute Inc. 2002-2008 “SAS OnlineDoc® 9.1.3” Available at: http://support.sas.com/onlinedoc/913


ACKNOWLEDGMENTS
Special heartfelt thanks to Jim Moore, IT Technician at BCB-ORS for his helpful hints and guidance.


CONTACT INFORMATION

Your comments and questions are valued and encouraged. Contact the author:

Teo Gamishev
Office of Research and Statistics
1919 Blanding Street
Columbia, SC 29201
gamishev@hotmail.com


SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS
Institute Inc. in the USA and other countries. ® indicates USA registration.

Other brand and product names are trademarks of their respective companies.

No comments:

Post a Comment