BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ColeG
Obsidian | Level 7

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.");
1 ACCEPTED SOLUTION

Accepted Solutions
ColeG
Obsidian | Level 7

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");

View solution in original post

6 REPLIES 6
Doc_Duke
Rhodochrosite | Level 12

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.

Julian_Visch
Calcite | Level 5

* 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;

 

Patrick
Opal | Level 21

@Julian_Visch wrote:

* Each attachment must have a separate attach statement;

 

 


@Julian_Visch

You can attach multiple documents via a single ATTACH statement as documented here:

http://go.documentation.sas.com/?docsetId=hostunx&docsetTarget=p1hl3t66coao7bn18vrmhx2gte1q.htm&docs...

 

 
ATTACH='filename.ext' | ATTACH= ('filename.ext' <attachment-options>)
specifies the physical names of the files to be attached to the message and any options to modify attachment specifications. Enclose filename.ext in quotation marks. To attach more than one file, enclose the group of filenames in parentheses. For example, attach='/u/userid/opinion.txt' and attach=("june11.txt" "july11.txt") are valid file attachments.

 

SamuelD
Fluorite | Level 6

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 🙂

ColeG
Obsidian | Level 7

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");
Patrick
Opal | Level 21

@ColeG

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 13795 views
  • 0 likes
  • 5 in conversation