Send Emails with Attachments via SAS

This tutorial explains how to send emails with attachments via SAS.

Send Emails with Attachments and cc Options
data _null_;
file sendit email
from="abc@provider.com"
to=("xyz@provider.com")
cc=("uvxyz@provider.com"
"pqrs@provider.com")
subject="Important Document"
importance="High"
attach=("C:\xxxx\Proc_logistic.xls");
put "Please find attached the file";
put;
put "Thanks!";
run;
Note : You can also add BCC option. For eg. bcc="abc@site.com"

If Error sending XLSX files
"Excel found unreadable content in "temp.xlsx". Do you want to recover the contents of this workbook? If you trust the source of this workbook, click yes. Excel cannot open the file "temp.xlsx" because the file format or the file extension is not valid. Verify that the file has not been corrupted and the the file extension matches the format of the file."

Solution : Use content_type option in the attach statement.
attach=("C:\xxxx\Proc_logistic.xlsx" content_type="application/xlsx")
List of Content_type options with file extensions

Extension Content_type
bmp image/bmp
csv application/vnd.ms-excel
doc application/msword
exe application/octet-stream
gif image/gif
htm application/html
html applicaton/html
jpeg image/jpeg
jpg image/jpeg
log text/plain
pdf application/pdf
png image/png
ppt application/vnd.ms-powerpoint
sas7bdat application/sas
tar application/x-tar
text text/plain
txt text/plain
xls application/excel
xlsx application/xlsx
zip application/x-zip-compressed

Send Multiple Attachments
data _null_;
file sendit email
from="abc@provider.com"
to=("xyz@provider.com")
cc=("uvxyz@provider.com"
"pqrs@provider.com")
subject="Important Document"
importance="High"
attach=("C:\Deepanshu\Proc_logistic.xlsx" content_type="application/xlsx"
"C:\Deepanshu\Summary.pdf" content_type="application/pdf"
"C:\Deepanshu\Summary.doc" content_type="application/word");
put "Please find attached the file";
put;
put "Thanks!";
put;
run;
Spread the Word!
Share
Related Posts
About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource.

10 Responses to "Send Emails with Attachments via SAS"
  1. Thanks for this tip! I will test it soon. Sunil Gupta

    ReplyDelete
  2. Thanks for this!

    ReplyDelete
  3. What if we want to send multiple SAS datasets into one excel file but into different sheets??

    ReplyDelete
    Replies
    1. You can create XLSX file using ODS output like below.

      ods excel file="C:\test\test1.xlsx" ;

      ods excel options(sheet_name="First Worksheet");
      proc print data=sashelp.cars noobs;
      run;

      ods excel options(sheet_name="Second Worksheet");
      proc print data=sashelp.class noobs;
      run;

      ods excel close;

      Delete
  4. Can we rename the attachment in attach clause?

    ReplyDelete
  5. excellent, it helped me a lot to send the logs to client.

    ReplyDelete
  6. Do you know how can we create password protected zip file from multiple Xlsx file and send it over email?

    ReplyDelete
  7. Thanks for your post. It helped me a lot when I try to send multiple XLSX files.

    ReplyDelete
  8. How to limit the size of attached excel to 50mb ?

    ReplyDelete

Next → ← Prev