My company has recently switched to SAS Enterprise Guide, and one of my responsibilities is to email out large batches of emails with multiple attachments. I know that to attach .xlsx files you need to add the line
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" lrecl=32000
to your attach= statement.
I have verified that each individual attachment works but when I try to send all attachments at once, only the last one attached can be opened. Every other attachment is corrupted and can't be viewed. Any help would be appreciated, thank you.
%macro emaildata (to_address, attachments);
Filename output email;
data _null_;
file output
to = (&to_address.)
subject = "Subject Line"
attach = (&attachments. content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" lrecl=32000);
put "text.";
run;
%mend emaildata;
/* Test */
%emaildata ('recipient', "&attach1." "&attach2." "&attach3." "&attach4." "&attach5." "&attach6.");
Hi,
Sorry to all for dropping off this chain. The solution I got from SAS is below. It is adaptable to any file type (again, I think this is for SAS EG only, I never had such complications with base SAS), can send different numbers of files to different recipients, can be sent to any number of recipients, and can be sent automatically through code without needing to go through the EG program builder or whatever it's called. I never got comfortable with it so I don't use it. You can drop any mimetypes that you don't need (I have only used PDF, CSV, XLSX, and DOCX, for example).
%macro emaildata (to_address, attachments);
proc format;
value $mimetypes
'csv' = 'text/csv'
'doc' = 'application/msword'
'docx' = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
'html' = 'text/html'
'jpeg' = 'image/jpeg'
'pdf' = 'application/pdf'
'ppt' = 'application/vnd.ms-powerpointtd'
'pptx' = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
'xls' = 'application/vnd.ms-excel'
'xlsx' = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
'xml' = 'application/xml'
'zip' = 'application/zip, application/x-compressed-zip'
;
run;
%local newAttach;
%do i = 1 %to 100 %by 2;
%let workingFile = %scan(&attachments,&i,%str(%'%"));
%put WorkingFile=&workingFile;
%if %bquote(&workingFile) ne %then
%do;
%put WorkingFile=&workingFile;
%let extension = %scan(&workingFile,-1,.);
%if %bquote(%sysfunc(putc(&extension,$mimetypes.))) ne Invalid %then
%let newAttach = &newAttach "&workingfile" content_type="%sysfunc(putc(&extension,$mimetypes.))";
%put newAttach = &newAttach;
%end;
%end;
Filename output email;
data _null_;
file output
to = (&to_address)
subject = "[secure] Subject"
attach = (&newAttach lrecl=32000);
put "Text";
run;
%mend emaildata;
/* Test */
%emaildata ('recipient@domain',
"file1.xlsx"
"file2.csv"
"file3.docx");
Cole,
I don't have an answer to using the macro.
Sometimes EGuide works better if you use its tools. If you have a file to send, right click on it and select 'send to' and then 'email as a step in the project'. Once you've added one file, you can add others. It's pretty flexible about sending data and reports.
{Added}. You need to do some setup in tools--> options and the mail is sent from your pc, not from the server.
* Each attachment must have a separate attach statement;
data _null_;
file outbox
to=(&email_to)
cc=(&email_cc)
From=(&email_from)
subject="Subject here"
attach=("file1.csv")
attach=("file2.csv");
put "*** My report *** ";
run;
@Julian_Visch wrote:
* Each attachment must have a separate attach statement;
You can attach multiple documents via a single ATTACH statement as documented here:
attach='/u/userid/opinion.txt'
and attach=("june11.txt" "july11.txt")
are valid file attachments.
Hi There
I was looking into this also, however i believe that the code suggested would need to be modified to:
data _null_;
file outbox
to=(&email_to)
cc=(&email_cc)
From=(&email_from)
subject="Subject here"
attach=("file1.csv" "file2.csv");
put "*** My report *** ";
run;
anyway the code i normally use in full is:
FILENAME Mailbox
EMAIL 'user@domain.com.au'
CC='user@domain2.com.au'
Subject='This is my report'
ATTACH=( 'path/file1.doc' 'path/file2.doc');
DATA _NULL_;
FILE Mailbox;
PUT "Email body text";
PUT "More email text";
RUN;
Hope this helps 🙂
Hi,
Sorry to all for dropping off this chain. The solution I got from SAS is below. It is adaptable to any file type (again, I think this is for SAS EG only, I never had such complications with base SAS), can send different numbers of files to different recipients, can be sent to any number of recipients, and can be sent automatically through code without needing to go through the EG program builder or whatever it's called. I never got comfortable with it so I don't use it. You can drop any mimetypes that you don't need (I have only used PDF, CSV, XLSX, and DOCX, for example).
%macro emaildata (to_address, attachments);
proc format;
value $mimetypes
'csv' = 'text/csv'
'doc' = 'application/msword'
'docx' = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
'html' = 'text/html'
'jpeg' = 'image/jpeg'
'pdf' = 'application/pdf'
'ppt' = 'application/vnd.ms-powerpointtd'
'pptx' = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
'xls' = 'application/vnd.ms-excel'
'xlsx' = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
'xml' = 'application/xml'
'zip' = 'application/zip, application/x-compressed-zip'
;
run;
%local newAttach;
%do i = 1 %to 100 %by 2;
%let workingFile = %scan(&attachments,&i,%str(%'%"));
%put WorkingFile=&workingFile;
%if %bquote(&workingFile) ne %then
%do;
%put WorkingFile=&workingFile;
%let extension = %scan(&workingFile,-1,.);
%if %bquote(%sysfunc(putc(&extension,$mimetypes.))) ne Invalid %then
%let newAttach = &newAttach "&workingfile" content_type="%sysfunc(putc(&extension,$mimetypes.))";
%put newAttach = &newAttach;
%end;
%end;
Filename output email;
data _null_;
file output
to = (&to_address)
subject = "[secure] Subject"
attach = (&newAttach lrecl=32000);
put "Text";
run;
%mend emaildata;
/* Test */
%emaildata ('recipient@domain',
"file1.xlsx"
"file2.csv"
"file3.docx");
Not sure but could it be that if you can't use default attachment options that you need to repeat them after every single attachment?
May be worth a test.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.