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

 

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;

  

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

14 REPLIES 14
Kurt_Bremser
Super User

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;
Babloo
Rhodochrosite | Level 12

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.
Kurt_Bremser
Super User

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.

Babloo
Rhodochrosite | Level 12
Could you please tell me how I can find the path for WORK data set?
RW9
Diamond | Level 26 RW9
Diamond | Level 26
attach="%sysfunc(pathname(work))/email.sas7bdat";

Of course you wont need that. 

Babloo
Rhodochrosite | Level 12

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";;
Kurt_Bremser
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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. 

Quentin
Super User

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.

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
Babloo
Rhodochrosite | Level 12

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?

 

 

 

Kurt_Bremser
Super User

@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.

Babloo
Rhodochrosite | Level 12

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.

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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:

https://communities.sas.com/t5/forums/searchpage/tab/message?advanced=false&allow_punctuation=false&...

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

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
  • 14 replies
  • 8012 views
  • 7 likes
  • 4 in conversation