|SAS Companion for UNIX Environments|
The SAS System lets you send electronic mail using SAS functions in a DATA step or in SCL. Sending e-mail from within the SAS System allows you to
|Initializing Electronic Mail|
Because of the wide range of e-mail
programs available, the
SAS System sends all mail through an external shell script. The SAS System
provides two scripts, located in
Note: You or your system administrator will probably have to customize these scripts
before you can use them with your specific e-mail program.
Specify the name of the script you will be using by setting the EMAILSYS system option. You can specify the EMAILSYS system option in the CONFIG.SAS file or when invoking your SAS session:
|Using the DATA Step or SCL to Send Electronic Mail|
In general, a DATA step or SCL code that sends electronic mail has the following components:
|Syntax of the FILENAME Statement for Electronic Mail|
To send electronic mail from a DATA step or SCL, issue a FILENAME statement of the following form:
|FILENAME fileref EMAIL 'address' <email-options>;|
The FILENAME statement accepts the following email-options:
to=('firstname.lastname@example.org' 'email@example.com')are valid TO values.
cc=('firstname.lastname@example.org' 'email@example.com')are valid CC values.
subject='June Report'are valid subjects. Any subject not enclosed in quotes is converted to upper case.
attach=('june98.txt' 'july98.txt')are valid file attachments.
Note: Not all external
scripts support file attachments or all types of file attachments. Scripts
that do not accept attachments should not send mail if an attachment is attempted.
Otherwise, the message could say "here's the graph you wanted," but the graph
would not be included.
You can also specify the email-options in the FILE statement inside the DATA step. Options that you specify in the FILE statement override any corresponding options that you specified in the FILENAME statement.
In your DATA step, after using the FILE statement to define your e-mail fileref as the output destination, use PUT statements to define the body of the message.
You can also use PUT statements to specify e-mail directives that change the attributes of your electronic message or perform actions with it. Specify only one directive in each PUT statement; each PUT statement can contain only the text associated with the directive it specifies.
The directives that change the attributes of your message are
The directives that perform actions are
|Example: Sending E-Mail from the DATA Step|
Suppose that you want to share a copy of your CONFIG.SAS file with your coworker Jim, whose user ID is JBrown. If your e-mail program handles alias names and attachments, you could send it by submitting the following DATA step:
filename mymail email 'JBrown' subject='My CONFIG.SAS file' attach='config.sas'; data _null_; file mymail; put 'Jim,'; put 'This is my CONFIG.SAS file.'; put 'I think you might like the new options I added.'; run;
The following example sends a message and two attached files to multiple recipients. It specifies the e-mail options in the FILE statement instead of the FILENAME statement:
filename outbox email 'firstname.lastname@example.org'; data _null_; file outbox to=('email@example.com' 'firstname.lastname@example.org') /* Overrides value in */ /* filename statement */ cc=('email@example.com' 'firstname.lastname@example.org') subject='My SAS output' attach=('results.out' 'code.sas') ; put 'Folks,'; put 'Attached is my output from the SAS program I ran last night.'; put 'It worked great!'; run;
You can use conditional logic in the DATA step to send multiple messages and control which recipients get which message. For example, suppose you want to send customized reports to members of two different departments. If your e-mail program handles alias names and attachments, your DATA step might look like the following:
filename reports email 'Jim'; data _null_; file reports; infile cards eof=lastobs; length name dept $ 21; input name dept; put '!EM_TO!' name; /* Assign the TO attribute */ put '!EM_SUBJECT! Report for ' dept; /* Assign the SUBJECT attribute */ put name ','; put 'Here is the latest report for ' dept '.'; if dept='marketing' then put '!EM_ATTACH! mktrept.txt'; else /* ATTACH the appropriate report */ put '!EM_ATTACH! devrept.txt'; put '!EM_SEND!'; /* Send the message */ put '!EM_NEWMSG!'; /* Clear the message attributes */ return; lastobs: put '!EM_ABORT!'; /* Abort the message before the */ /* RUN statement causes it to */ /* be sent again. datalines; Susan marketing Jim marketing Rita development Herb development ; run;
The resulting e-mail message and its attachments are dependent on the department to which the recipient belongs.
Note: You must use the !EM_NEWMSG! directive to clear the message attributes between
recipients. The !EM_ABORT! directive prevents the message from being automatically
sent at the end of the DATA step.
|Example: Sending E-Mail Using SCL Code|
The following example is the SCL code behind a frame entry design for e-mail. The frame entry includes several text entry fields that let the user enter information:
|mailto||the user ID to send mail to|
|copyto||the user ID to copy (CC) the mail to|
|attach||the name of a file to attach|
|subject||the subject of the mail|
|line1||the text of the message|
The frame entry also contains a pushbutton called SEND
that causes this SCL code (marked by the
send: label) to execute.
send: /* set up a fileref */ rc = filename('mailit','userid','email'); /* if the fileref was successfully set up open the file to write to */ if rc = 0 then do; fid = fopen('mailit','o'); if fid > 0 then do; /* fput statements are used to implement writing the mail and the components such as subject, who to mail to, etc. */ fputrc1 = fput(fid,line1); rc = fwrite(fid); fputrc2 = fput(fid,'!EM_TO! '||mailto); rc = fwrite(fid); fputrc3 = fput(fid,'!EM_CC! '||copyto); rc = fwrite(fid); fputrc4 = fput(fid,'!EM_ATTACH! '||attach); rc = fwrite(fid); fputrc5 = fput(fid,'!EM_SUBJECT! '||subject); rc = fwrite(fid); closerc = fclose(fid); end; end; return; cancel: call execcmd('end'); return;
Top of Page
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.