BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello,

I want to send email from SAS and in the email will have:

1-Attach XLSX file with summary data (summary data set)

2-Attach XLSX file with raw data (sashelp.cars data set)

3-In email Body print summary data (summary data set)

 

In the code below I attach one file (summary data)

My question-What is the way to attach also raw data (sashelp.cars data set)?


proc sql;
create table summary as
select type,
       sum(invoice) as sum_invoice
from  sashelp.cars
group by type
;
quit;


/**CREATE EXCEL FILE***/
ODS EXCEL FILE="/usr/local/SAS/SASUsers/LabRet/UserDir/udclk79/summary_Report_Cars.xlsx" style=htmlblue options(
frozen_headers="ON" 
/*autofilter = "1-17" */
row_heights="30,13,13,13,13,13,13"
flow = "header, data" 
absolute_column_width = "18,20,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10" 
sheet_name = "Cars"
) ;
title;
footnote;
title1 'Smmary of cars';
proc print data=summary  noobs;
Run;
ods excel close;

FILENAME mail EMAIL 
TO=("Ron.gdansk@gmail.com")
FROM='SAS  <Ron.Einstein@BankLeumi.co.il>'
SUBJECT="Cars"
encoding='utf-8'   
CONTENT_TYPE="text/html" 
encoding="utf-8"
attach=("/usr/local/SAS/SASUsers/LabRet/UserDir/udclk79/summary_Report_Cars.xlsx" content_type="excel");
/*******Here I want to attach also another data set called sashelp.cars****/
ODS LISTING CLOSE;
ODS HTML BODY=mail;
/*** EMAIL BODY TEXT**********/
title;
footnote;
title  JUSTIFY=center color="purple" height=25pt bold italic underlin=1  'Cars';
proc report data=summary nowd;
column _all_;
run;

 

5 REPLIES 5
Kurt_Bremser
Super User

As almost always, Maxim 1.

From the documentation of FILENAME Statement, EMAIL method:

ATTACH='filename.ext' | ATTACH= ('filename.ext'attachment-options)

specifies the physical name of the file or files to be attached to the message and any options to modify attachment specifications. The physical name is the name that is recognized by the operating environment. Enclose the physical name in quotation marks. To attach more than one file, enclose the group of files in parentheses, enclose each file in quotation marks, and separate each with a space. Here are examples:

For the raw data, use PROC EXPORT to create the second Excel file.

Mazi
Pyrite | Level 9

Hi, I tried attaching using the filename statement and could not get it to work despite specifying content_type as application/x-sas-data. 
I received the following error: 39996 - "ERROR: An attachment record was truncated" when sending files as email attachments

ERROR: An attachment record was truncated. The attachment LRECL is too small.
FILENAME mail EMAIL 
TO=("johndoe@email.com")
FROM=("johndoe@email.com")
SUBJECT="Cars"
encoding='utf-8'
CONTENT_TYPE="text/html"
encoding="utf-8"
lrecl=32767
attach=("%sysfunc(pathname(work))/cars.sas7bdat" content_type="application/x-sas-data");
/*******Here I want to attach also another data set called sashelp.cars****/

Data _null_;
	file mail;
	put "Testing";
	put "Testing";
run;

 Though, I was wondering if you could try using the mailx command in conjunction with %sysexec macro statement instead?

Patrick
Opal | Level 21

I don't have an environment where I could try it myself but just looking at your code, the referenced usage note and doing some further Internet research I can't see anything wrong in your code. 

I couldn't find any confirmed solution anywhere how to send a single .sas7bdat as email attachment. 

I believe what would add value to the forums here: Contact SAS Tech Support directly and then post the final answer/resolution back here and mark it as solution/answer as provided by SAS Tech Support.

yabwon
Onyx | Level 15

Try SAS7BDAT:

FILENAME mail EMAIL 
TO=("xxx@email.com")
FROM=("yyyy@email.com")
SUBJECT="Cars"
encoding='utf-8'
CONTENT_TYPE="text/html"
encoding="utf-8"
attach=(
"%sysfunc(pathname(work))/cars.sas7bdat" 
content_type="application/SAS7BDAT" /* <-----------------------*/
)
;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Mazi
Pyrite | Level 9
ods excel file = "~/test.xlsx";
	proc print data=sashelp.cars;
	run;
ods excel close;

proc copy in=sashelp out=work;
	select cars;
run;

FILENAME mail EMAIL 
TO=("xxx@email.com")
FROM=("xxx@email.com")
SUBJECT="Cars"
encoding='utf-8'
CONTENT_TYPE="text/html"
encoding="utf-8"
attach=(
"%sysfunc(pathname(work))/cars.sas7bdat" 
content_type="application/SAS7BDAT" "~/test.xlsx" content_type="excel"
)
;

data _null_;
	file mail;
	Put "Testing";
	Put "Testing";
run;

This seems to have worked for me. 
Thank you @yabwon for the suggestion.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 999 views
  • 2 likes
  • 5 in conversation