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

Hi guys!

 

I am using the following script to send e-mails:

    Filename    OK&id
                    Email   From    =    ("&SENDER")
                            Sender  =    ("&SENDER")
                            To      =    ("&RECEIVER" )
                            Subject =    "&SUBJECT"
                            ATTACH  =    ("&AttachmentPath")
                            ;
    data _null_;
           file OK&id;
 
           PUT "&Content"
            //;
    run;

In the log file I see the following:

NOTE: 3 records were written to the file OK4.
      The minimum record length was 0.
      The maximum record length was 19.

 

I want to execute a subsequent data step which is based on if recrods were written to the file because if the mail part fails there's no records written to the file (or at least indicated in the log). Is there a way for me to check how many records are in the file OK4 in my code?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You can test if the file exists BEFORE trying to attach it to the email.

%if %sysfunc(fileexist("&AttachmentPath")) %then %do;
... send email ...
%end;
%else %do;
... write note ...
%end;

View solution in original post

8 REPLIES 8
Shmuel
Garnet | Level 18

You can use

proc printto log="<log file path and name>.log"; run;

     *your code written here;

proc printto; run;

data _null_;
   infile "<log file path and name>.log";
   input line $100.;
    if index(line,"Records") then do;
      /* extract number of records */
   end;
run;
andreas_lds
Jade | Level 19

You need to post more details of the code you are running, the step you have posted imho can't write 0 obs.

sasnewbiexx
Calcite | Level 5

Well, my question is related to this specific part and I don't have a code afterwards (that's what I'm asking for). If the attachment is correct it will give me the log that I posted. Let's say the attachment file is deleted by a user, then it will print the following in the log: 

NOTE: The file OK4 is:
      E-Mail Access Device
ERROR: Error opening attachment file D:\\TestAttc.xlsx.
ERROR: Physical file does not exist, D:\\TestAttc.xlsx.
 
So my overall goal is to add code and try and check if records have been written or not to the OK4 file.
Tom
Super User Tom
Super User

You can test if the file exists BEFORE trying to attach it to the email.

%if %sysfunc(fileexist("&AttachmentPath")) %then %do;
... send email ...
%end;
%else %do;
... write note ...
%end;
Shmuel
Garnet | Level 18

@sasnewbiexx wrote:

Well, my question is related to this specific part and I don't have a code afterwards (that's what I'm asking for). If the attachment is correct it will give me the log that I posted. Let's say the attachment file is deleted by a user, then it will print the following in the log: 

NOTE: The file OK4 is:
      E-Mail Access Device
ERROR: Error opening attachment file D:\\TestAttc.xlsx.
ERROR: Physical file does not exist, D:\\TestAttc.xlsx.
 
So my overall goal is to add code and try and check if records have been written or not to the OK4 file.

Please post the full log.

It is not clear what you have run.

 

Pay attention OK4 and the log file created by proc printto are different files.

Running the code as you first posted should crate a file probably like

<sender mail>
<recipient mail>
<contents given>
<space line as a result of PUT ...//> 

 

sasnewbiexx
Calcite | Level 5
NOTE: Remote submit to MYNODE commencing.
247   Filename OK&id Email From = ("&SENDER") Sender = ("&SENDER") To = ("&RECEIVER" ) Subject =
247! "&SUBJECT" ;
248   data _null_;
249   file OK&id;
250   PUT "&Content" //;
251   run;
NOTE: The file OK4 is:
      E-Mail Access Device
Message sent
      To:          "email@email.com"
      Cc:
      Bcc:
      Subject:     TestExternal
      Attachments:
NOTE: 3 records were written to the file OK4.
      The minimum record length was 0.
      The maximum record length was 19.
NOTE: DATA statement used (Total process time):
      real time           0.18 seconds
      cpu time            0.00 seconds

This is the log. Can this file OK4 be saved somewhere (in a library) or be read from a the code?

andreas_lds
Jade | Level 19

@sasnewbiexx wrote:

Well, my question is related to this specific part and I don't have a code afterwards (that's what I'm asking for). If the attachment is correct it will give me the log that I posted. Let's say the attachment file is deleted by a user, then it will print the following in the log: 

NOTE: The file OK4 is:
      E-Mail Access Device
ERROR: Error opening attachment file D:\\TestAttc.xlsx.
ERROR: Physical file does not exist, D:\\TestAttc.xlsx.
 
So my overall goal is to add code and try and check if records have been written or not to the OK4 file.

So you don't need to run code after sending the mail, you need to change the code, so that the mail is only send, if the attachment exists. @Tom posted a solution for this problem.

sasnewbiexx
Calcite | Level 5
Yes, you are right @andreas_lds. @Tom's answer actually does the job for me! Thank you very much for the help!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 8 replies
  • 747 views
  • 3 likes
  • 4 in conversation