- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I want to send an email via SAS. If the condition (see the if clause below) from below program is true I want the value of the put statement in the email otherwise 'crefius' dataset to attached in the email. . Could someone of you assist me tweaking the code below to send an email only if the condition is true? I'm OK to convert the SAS dataset to flat files to attach in the email if there is no way to attach a SaS dataset in the email.
filename mymail email ("babloo.kiya@xxx.com")
subject='CREFIUS Table status' attach='crefius';
data email (rename=card=Num_of_observations rename=lastused=Processed_Date drop=curr_month);
file mymail;
set Crefius;
curr_month=intnx('month',today(),0);
format curr_month date9.;
if month(lastused) ne month(curr_month) then
put 'Inspect the Crefius Tables';
run;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
First of all, it is rarely a good idea to attach data to an email:
- emails are NOT safe, unless you have end-to-end encryption enforced (not enabled, ENFORCED!)
- emails only work up to a given size (dependent on the mailserver settings of the recipient, and are rejected if that size is exceeded)
- still, large emails clutter up the mailbox, and you won't make friends of the admin(s) of the recipient(s) if you repeatedly cause trouble
Put the data in a safe and accessible location (reachable through https/sftp/ftps/ssh) for download, and just send a notification message:
data _null_;
set Crefius (obs=1);
curr_month=intnx('month',today(),0);
format curr_month date9.;
if month(lastused) ne month(curr_month)
then call symput('message','Inspect the Crefius Tables');
else call symput('message','Crefius Tables ready for download');
run;
filename mymail email ("babloo.kiya@xxx.com")
subject='CREFIUS Table status';
data _null_;
file mymail;
put "&message.";
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
First of all, it is rarely a good idea to attach data to an email:
- emails are NOT safe, unless you have end-to-end encryption enforced (not enabled, ENFORCED!)
- emails only work up to a given size (dependent on the mailserver settings of the recipient, and are rejected if that size is exceeded)
- still, large emails clutter up the mailbox, and you won't make friends of the admin(s) of the recipient(s) if you repeatedly cause trouble
Put the data in a safe and accessible location (reachable through https/sftp/ftps/ssh) for download, and just send a notification message:
data _null_;
set Crefius (obs=1);
curr_month=intnx('month',today(),0);
format curr_month date9.;
if month(lastused) ne month(curr_month)
then call symput('message','Inspect the Crefius Tables');
else call symput('message','Crefius Tables ready for download');
run;
filename mymail email ("babloo.kiya@xxx.com")
subject='CREFIUS Table status';
data _null_;
file mymail;
put "&message.";
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I'm receiving the error as follows. May I know what might be the likely cause for this error?
50 filename mymail email ("babloo.kiya@xxx.com")
51 subject='CREFIUS Table status' attach=email.sas7bdat;
52
53 data email (rename=card=Num_of_observations rename=lastused=Processed_Date drop=curr_month);
54 /*file mymail;*/
55 set Crefius;
56 curr_month=intnx('month',today(),0);
57 format curr_month date9.;
58 if month(lastused) ne month(curr_month) then
59 call symput('message','Inspect the Crefius Tables');
60 else call symput('message','Crefius Tables ready for download');
61 run;
NOTE: There were 15 observations read from the data set WORK.CREFIUS.
NOTE: The data set WORK.EMAIL has 15 observations and 3 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds
62
63
64 data _null_;
65 file mymail;
66 set email;
67 put "&message.";
68 run;
NOTE: The file MYMAIL is:
E-Mail Access Device
ERROR: Error opening attachment file EMAIL.SAS7BDAT.
ERROR: Physical file does not exist, /GTU/AGSR/bin1/SASConfigFolder/Lev2/SASDIT/EMAIL.SAS7BDAT.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
DO NOT SEND DATA VIA EMAIL!
DO NOT SEND DATA VIA EMAIL!
DO NOT SEND DATA VIA EMAIL!
DO NOT SEND DATA VIA EMAIL!
DO NOT SEND DATA VIA EMAIL!
I hope you get the message now.
That said, you need to supply an absolute path for attachments, otherwise SAS goes looking in the current working directory.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
attach="%sysfunc(pathname(work))/email.sas7bdat";
Of course you wont need that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I'm receiving this error after updating the filename statement.
RROR: Error opening attachment file
/GTU/AGSR/wrk1/SAS_workDDAB00005A48_dlusas003g.linux.dv.be/SAS_work2F2C00005A48_dlu0usas003g.linux.dv.be/email.sas7bdat.
ERROR: Physical file does not exist,
/GTU/AGSR/wrk1/SAS_workDDAB00005A48_dlusas003g.linux.dv.be/SAS_work2F2C00005A48_dlu0usas003g.linux.dv.be/email.sas7bdat.
Filename statement is,
filename mymail email ("babloo.kiya@xxx.com")
subject='CREFIUS Table status' attach="%sysfunc(pathname(work))/email.sas7bdat";;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
When SAS tells you that a file is not there, it's not there, period. Make sure that you really create that dataset as work.email before you try to send it.
Keep an eye on your infrastructure; if you're working on a grid, the task that created the dataset may have run on a different node than the one that sends the email.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This:
/GTU/AGSR/wrk1/SAS_workDDAB00005A48_dlusas003g.linux.dv.be/SAS_work2F2C00005A48_dlu0usas003g.linux.dv.be/email.sas7bdat.
Does not look right, you have a path, then some what I guess to be some sort of url or something with the linux.dv.be.
Afraid you need to find what the real path is on your system, I do not know what your setup is.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I guess that, depending on the system setup, SAS might use the fully qualified domain name of the server in the directory name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I suppose you could try checking that the data set exists with code like:
data _null_ ;
set "%sysfunc(pathname(work))/email.sas7bdat" ;
run ;
Or maybe better, look through the file system to make your email.sas7bdat exists in the directory, and has the correct extension etc.
Next up: Troy Martin Hughes presents Calling Open-Source Python Functions within SAS PROC FCMP: A Google Maps API Geocoding Adventure on Wednesday April 23.
Register now at https://www.basug.org/events.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I tried the code as you mentioned and the log says,
59 data _null_ ;
60 set "%sysfunc(pathname(work))/email.sas7bdat" ;
61 run ;
NOTE: There were 15 observations read from the data set
/GTU/AGSR/wrk1/SAS_workC2A2000014B9_dlu0usas003g.linux.dv.be/SAS_workB91E000014B9_dlu0usas003g.linux.dv.be/email.sas7bdat.
NOTE: DATA statement used (Total process time)
Then I modified the 'attach' option inn Filename statement as follows.
filename mymail email ("babloo.kiya@xxx.com")
subject='CREFIUS Table status' attach='/GTU/AGSR/wrk1/SAS_workC2A2000014B9_dlu0usas003g.linux.dv.be/SAS_workB91E000014B9_dlu0usas003g.linux.dv.be/email.sas7bdat';
But still I end up with the same error.
50 data email (rename=card=Num_of_observations rename=lastused=Processed_Date drop=curr_month);
51 file mymail;
52 set Crefius;
53 curr_month=intnx('month',today(),0);
54 format curr_month date9.;
55 if month(lastused) ne month(curr_month) then
56 put 'Inspect the Crefius Tables';
57 run;
NOTE: The file MYMAIL is:
E-Mail Access Device
ERROR: Error opening attachment file
/GTU/AGSR/wrk1/SAS_workC2A2000014B9_dlu0usas003g.linux.dv.be/SAS_workB91E000014B9_dlu0usas003g.linux.dv.be/email.sas7bdat.
ERROR: File is in use,
/GTU/AGSR/wrk1/SAS_workC2A2000014B9_dlu0usas003g.linux.dv.be/SAS_workB91E000014B9_dlu0usas003g.linux.dv.be/email.sas7bdat.
Could you please help me understand how to find the file system to know the right path for WORK dataset?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@Babloo wrote:
I tried the code as you mentioned and the log says,
59 data _null_ ; 60 set "%sysfunc(pathname(work))/email.sas7bdat" ; 61 run ; NOTE: There were 15 observations read from the data set /GTU/AGSR/wrk1/SAS_workC2A2000014B9_dlu0usas003g.linux.dv.be/SAS_workB91E000014B9_dlu0usas003g.linux.dv.be/email.sas7bdat. NOTE: DATA statement used (Total process time)
Then I modified the 'attach' option inn Filename statement as follows.
filename mymail email ("babloo.kiya@xxx.com") subject='CREFIUS Table status' attach='/GTU/AGSR/wrk1/SAS_workC2A2000014B9_dlu0usas003g.linux.dv.be/SAS_workB91E000014B9_dlu0usas003g.linux.dv.be/email.sas7bdat';
But still I end up with the same error.
50 data email (rename=card=Num_of_observations rename=lastused=Processed_Date drop=curr_month); 51 file mymail; 52 set Crefius; 53 curr_month=intnx('month',today(),0); 54 format curr_month date9.; 55 if month(lastused) ne month(curr_month) then 56 put 'Inspect the Crefius Tables'; 57 run; NOTE: The file MYMAIL is: E-Mail Access Device ERROR: Error opening attachment file /GTU/AGSR/wrk1/SAS_workC2A2000014B9_dlu0usas003g.linux.dv.be/SAS_workB91E000014B9_dlu0usas003g.linux.dv.be/email.sas7bdat. ERROR: File is in use, /GTU/AGSR/wrk1/SAS_workC2A2000014B9_dlu0usas003g.linux.dv.be/SAS_workB91E000014B9_dlu0usas003g.linux.dv.be/email.sas7bdat.
Could you please help me understand how to find the file system to know the right path for WORK dataset?
Pay attention to log details. Now it does not complain about a file not found, but a file in use. That's because you use the dataset both as attachment and as source dataset in the set statement.
I already showed you how to make the decision in a previous step and save the message to a macro variable.
There's also not much sense putting the message for every observation of crefius, so it's not needed in the data step that actually sends the mail.
Ceterum censeo: do not send datasets as attachments.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I just noticed that folder for WORK dataset changes everytime. So I tried to tweak the 'attach' option as follows, but I ended up with error.
filename mymail email ("baloo.kiya@xxx.com")
subject='CREFIUS Table status' attach='%sysfunc(pathname(work))/email.sas7bdat';
Log:
60 data _null_ ;
61 file mymail;
62 put "&message.";
63 run ;
NOTE: The file MYMAIL is:
E-Mail Access Device
ERROR: Error opening attachment file %sysfunc(pathname(work))/email.sas7bdat.
ERROR: Physical file does not exist, /GTU/AGSR/bin1/SASConfigFolder/Lev2/SASDIT/%sysfunc(pathname(work))/email.sas7bdat.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If you use the search functionality on here before posting, you will see that that exact question has been asked several times recently, as recent as last week: