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

Hi, 

 

I need some help, I am building a code that extract errors from sas log and send it out by email, by now the program is working good but I need to do some adjustments. The first one is that I need to have the log both in sas program and in the external file, I put the code below but when I send the log back to the default location the external file return in blank.

 

 

PROC PRINTTO LOG="/sasdata/user_Data/log.log" NEW;
RUN;

PROC PRINTTO; /*Routing back the log the default location*/
RUN;

The second approach is how could I do for just to send the email when an error is found:

 

 filename mylog "/sasdata/user_Data/log.log";       

 filename report "/sasdata/user_Data/reportlog.txt" ;   
data _null_; infile mylog; input; lineno+1; file report; rec = _infile_; foundit = index(rec,"ERROR:"); if ( foundit > 0 ) then do; put lineno " " rec; end; foundit = index(rec,"ERROR "); if ( foundit > 0 ) then do; rec=substr(rec,foundit); if length(rec) > 15 then do; tempstr=substr(rec,1,15); if (substr(tempstr,7,1) >= "0" and substr(tempstr,7,1) <= "9" and index(tempstr,"-") > 0 and index(tempstr,":") > 0 ) then do; put lineno " " rec; end; end; end; foundit = index(rec,"WARNING:"); if ( foundit > 0 ) then do; put lineno " " rec; end; foundit = index(rec,"WARNING "); if ( foundit > 0 ) then do; rec=substr(rec,foundit); if length(rec) > 15 then do; tempstr=substr(rec,1,15); if (substr(tempstr,9,1) >= "0" and substr(tempstr,9,1) <= "9" and index(tempstr,"-") > 0 and index(tempstr,":") > 0 ) then do; put lineno " " rec; end; end; end; foundit = index(rec,"ERROR ["); if ( foundit > 0 ) then do; put lineno " " rec; end; foundit = index(rec,"WARN ["); if ( foundit > 0 ) then do; put lineno " " rec; end; run; /*SEND E-MAIL*/ filename outbox email attach=("/sasdata/u24807_Data/reportlog.txt") to='chrodriguez@' type='text/html' subject= "Log ETL" from='example@'; DATA _NULL_; FILE outbox; PUT "<body>"; PUT "<p>Hello, </p>"; PUT "<p>Program executed with errors.</p>"; PUT "<p>Regards.</p>"; PUT "</body>"; RUN;

Any help will be pretty appreciated.
1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

Why not just "replay" it back to the log after you close it.

 

filename mylog "/sasdata/user_Data/log.log";       
data _null_; *Replay captured log;
   infile mylog;
   input;
   put _infile_;
   run;

View solution in original post

9 REPLIES 9
Astounding
PROC Star

For your first question, you are using the wrong tool.  PROC PRINTTO can redirect the log to a different destination.  But you still only get one copy of the log (as you have seen).  To get an additional copy of the log in another location, take a look at the ALTLOG option.

rodrichiez
Quartz | Level 8

Thank you @Astounding I'll read about the ALTLOG option.

rodrichiez
Quartz | Level 8

Hi Astounding. I just read about altlog and seems to be a command for use in the config file not in a sas program. When I try to run the code with -ALTLOG /sasdata/User_Data/log.log , SAS doesn't recognize the command.

data_null__
Jade | Level 19

Why not just "replay" it back to the log after you close it.

 

filename mylog "/sasdata/user_Data/log.log";       
data _null_; *Replay captured log;
   infile mylog;
   input;
   put _infile_;
   run;
rodrichiez
Quartz | Level 8
Hi data_null. I tried your example but code seems to stuck in a loop.
rodrichiez
Quartz | Level 8
Update: I kept trying the code sent by data_null and actually is working fine. Thanks @Data_null_. But I;m still haven't found solution for my 2nd question.
data_null__
Jade | Level 19

@rodrichiez wrote:
But I;m still haven't found solution for my 2nd question.

 

The second approach is how could I do for just to send the email when an error is found:

 


You can check SYSRC and SYSCC.  Also automatic variables

 

AUTOMATIC SYSERRORTEXT 180-322: Statement is not valid or it is used out of proper order.
AUTOMATIC SYSWARNINGTEXT Data set WORK.WANT was not replaced because this step was stopped.
rodrichiez
Quartz | Level 8

@data_null__ Ok, I'll search about it. But I was thinking in add to the code something like:

 

 if ( foundit <= 0 ) then do; 
   break;
   end;

Yet I don't know if there is something similar to break in SAS to stop job execution. 

rodrichiez
Quartz | Level 8
Thank you @Data_null_ . I have been reading about it and I have done a significant advance to accomplish what I want using SYSERR and SYSCC.

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!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 9 replies
  • 2687 views
  • 3 likes
  • 3 in conversation