Chapter Contents

Previous

Next
SAS Companion for the OpenVMS Operating Environment

Sending Electronic Mail from Within the SAS System (EMAIL)

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


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 using the following syntax:

FILENAME fileref EMAIL 'address' <email-options>
where

fileref
is a valid fileref.

'address'
is the destination e-mail address of the user to whom you want to send e-mail. You must specify an address here, but you can override its value with the TO e-mail option.

email-options
can be any of the following:

TO=to-address
specifies the primary recipients of the electronic mail. If an address contains more than one word, you must enclose it in double quotation marks. To specify more than one address, you must enclose the group of addresses in parentheses and enclose each address in double quotation marks. For example, to="joe@somplace.org"and to=("joe@smplc.org" "jane@diffplc.org")are valid TO values.

CC=cc-address
specifies the recipients you want to receive a copy of the electronic mail. If an address contains more than one word, you must enclose it in double quotation marks. To specify more than one address, you must enclose the group of addresses in parentheses and enclose each address in double quotations marks. For example, cc="joe@somplace.org"and cc=("joe@smplc.org" "jane@diffplc.org")are valid CC values.

SUBJECT='subject'
specifies the subject of the message. If the subject text is longer than one word, you must enclose it in single quotation marks. For example, subject=Sales and subject='June Report'are valid SUBJECT values. Any subject text not enclosed in quotation marks is converted to uppercase.

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 changes 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 following:

!EM_TO! addresses
replaces the current primary recipient addresses with addresses. In the PUT statement, specify addresses without single quotation marks.

!EM_CC! addresses
replaces the current copied recipient addresses with addresses. In the PUT statement, specify addresses without single quotation marks.

!EM_SUBJECT! subject
replaces the current subject of the message with subject.

The directives that perform actions are the following:

!EM_SEND!
sends the message with the current attributes. By default, the message is automatically sent at the end of the DATA step. If you use this directive, the SAS System sends the message when it encounters the directive, and again at the end of the DATA step.

!EM_ABORT!
aborts the current message. You can use this directive to stop the SAS System from automatically sending the message at the end of the DATA step.

!EM_NEWMSG!
clears all attributes of the current message, including TO, CC, SUBJECT, and the message body.


Example: Sending E-Mail from the DATA Step

Suppose that you want to tell your coworker Jim, whose user ID is JBrown, about some changes you made to your CONFIG.SAS file. If your e-mail program handles alias names, you could send it by submitting the following DATA step:

filename mymail email 'JBrown'
         subject='My CONFIG.SAS file';

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 to multiple recipients. It specifies the email-options in the FILE statement instead of the FILENAME statement:

filename outbox email 'ron@acme.com';

data _null_;
  file outbox
     to=("ron@acme.com" "lisa@acme.com")
     /* Overrides the value in  */
     /* the filename statement. */

     cc=("margaret@yourcomp.com"
         "lenny@laverne.abc.com")
     subject='My SAS output';
  put 'Folks,';
  put 'Take a look at 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 messages. For example, suppose you want to notify members of two different departments that their customized reports are available. If your e-mail program handles alias names, your DATA step might look like the following:

filename reports email 'Jim'

data _null_;
  file reports;
  infile cards eof=lastogs;
  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 'ATTN: Sales Representatives'
  else
    put 'For Your Information'

  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.  [cautionend]


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
subject the subject of the mail message
line1 the text of the mail 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_SUBJECT! '||subject);
         rc = fwrite(fid);

         closerc = fclose(fid);
      end;
   end;
return;

cancel:
   call execcmd('end');
return;


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.