Jedi SAS Tricks: Email from the Front – Part 2


In my last blog post, I discussed how to use SAS options to send unattended email using an SMTP server. In this segment, I’ll tackle creating “pretty” email (using HTML) and sending out emails with attachments. First, I produced a basic report using ODS. I liked the look, and set out to replicate it in the body of my email.


Building on the basic email code we used last time, I added adding the report code and ODS HTML statements. Here is the code:

  /** Set system email options **/
options emailsys=SMTP EMAILID="" 

  /** Put message, summary and detail all results in the email body **/
filename mymail email "" 
    subject="Report for sashelp.class" lrecl=256 type="TEXT/HTML";

ods _all_ close;
ods html body=mymail style=sasweb;
options nocenter;
  /** Start writing the email in a DATA _NULL_ step **/
data _null_;
   file print;
   put "Here is the information you asked for.";
   put "Stay SASy!";
   put "Mark";
  /** Insert query results **/
title "Stats for sashelp.class where age > 10";
   SELECT COUNT(*) 'Head Count'
        , Avg(age) 'Average Age' format=5.1
        , Avg(height) 'Average Height' format=5.1
        , Avg(weight) 'Average Weight' format=5.1
      from sashelp.class
      where age > 10
proc sql;
title "Details from sashelp.class where age > 10";
   SELECT from sashelp.class where age > 10; 
ods _all_ close; 
ods listing; 

And, to my surprise, the email body looked different from the report:


Well, I didn’t care for the look of the email, and as I rummaged through the site I found a suggestion to use HTML3 instead of the default HTML destination (which produces HTML4 markup), due to a change in the way the MS Outlook renders Cascading Style Sheets. So I modified the program to use HTML3, and the results were just as I wanted.

Next I tried just writing the note and summary to the email body, with the detail report attached as a PDF file. This video shows how:

As usual, the SAS code I used in the video is included at the bottom of this post.
Until next time, may the SAS be with you!

Emailing from SAS ZIP file


About Author

SAS Jedi

Principal Technical Training Consultant

Mark Jordan (a.k.a. SAS Jedi) grew up in northeast Brazil as the son of Baptist missionaries. After 20 years as a US Navy submariner pursuing his passion for programming as a hobby, in 1994 he retired, turned his hobby into a dream job, and has been a SAS programmer ever since. Mark writes and teaches a broad spectrum of SAS programming classes, and his book, "Mastering the SAS® DS2 Procedure: Advanced Data Wrangling Techniques" is in its second edition. When he isn’t writing, teaching, or posting “Jedi SAS Tricks”, Mark enjoys playing with his grand and great-grandchildren, hanging out at the beach, and reading science fiction novels. His secret obsession is flying toys – kites, rockets, drones – and though he usually tries to convince Lori that they are for the grandkids, she isn't buying it. Mark lives in historic Williamsburg, VA with his wife, Lori, and Stella, their cat. To connect with Mark, check out his SAS Press Author page, follow him on Twitter @SASJedi or connect on Facebook or LinkedIn.


  1. Just to let you know, the technology used to produce the example was:
    - SAS 9.2 on Windows (tested in both 32 bit and 64 bit).
    - The email was read and rendered OK using
    - Outlook 2010 (Microsoft Exchange back end)
    - Android Mail app on HTC Inspire
    - Gmail web app
    Possible SAS code issues:
    The most common SAS coding error with HTML formatted email which causes the effect you are describing is to leave off the TYPE= option in the FILENAME statement. TYPE must be set to "TEXT/HTML" or the email does not display properly HTML in the email client.
    An example of correct FILENAME syntax is included here:
    filename mymail email
    subject="Report for sashelp.class"
    lrecl=256 type="TEXT/HTML";
    The next most common problem it to use an ODS HTML statement instead of ODS HTML3 in SAS9. The results won't display properly in Outlook if you don't specify HTML3.
    Let me know if this information was enough to get you rolling, or if you are still having difficulty with your code.

    • no luck for me either :(
      i've done everything above and i got plain text :(
      maybe the fact that my outlook uses exchange server instead of smtp causes the problem :(


      • This post was written about 7 years ago, and email clients such as Outlook have changed significantly since then. Email generated with the ODS HTML destination doesn't render, and some HTML3 tags are improperly rendered as text. The HTML5 ODS destination is the most successfully rendered by both Outlook 2016 and Gmail, so that's the destination I would recommend for now. Otherwise, the code works as-is (as long as you properly authenticate to your SMTP server).

        May the SAS be with you!

  2. ITs not working for me. In the mail I see only html texts. HTML is not redereed as HTML instead all the html codes are displayed in the body of the email. I used the exact code and tried it. Please let me know if any other settings required.

Back to Top