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.
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;
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.
Thank you @Astounding I'll read about the ALTLOG option.
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.
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 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.
@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.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.