The SAS Output Delivery System and reporting techniques

Extract Errors and Warnings from log

Reply
Contributor
Posts: 28

Extract Errors and Warnings from log

I'm trying to extract errors and warnings from the log file. There are a lot of options to suppress messages, but I want both the full log as the warnings/errors.

Obviously I could run some regex on the logfile with some script, but I want this to happen in SAS.

Currently the full log is being written to an output file with

dm 'log; file "&out_dir\saslog.log" replace';

 

So either I can import this log file again and run some regex commands or, preferably, directly extract warnings and errors from the log file.

Is there an option in the log command or some other command to extract warnings/errors?

 

Thanks!

Trusted Advisor
Posts: 1,572

Re: Extract Errors and Warnings from log

Posted in reply to WouterKBC

log is a text file, so you can write a small SAS program to read it and look for some strings, like:

 

data my_log;

     infile  "&out_dir\saslog.log" truncover;

   input a_line $200.;

   if index(a_line, 'ERROR:') > 0 or

      index(a_line, 'WARNING') > 0

   then output;

run;

 

Depending on your system beaware of upcase/lowcase while defining strings to look for.

Contributor
Posts: 28

Re: Extract Errors and Warnings from log

Awesome!

But doesn't this solution only work for single lines? What if the error/warning is printed over multiple lines?

Trusted Advisor
Posts: 1,572

Re: Extract Errors and Warnings from log

Posted in reply to WouterKBC

The program will sellect all  the lines that contain the strings you scaned for.

 

Better try it and see the results. Addapt to the strings you are interested in.

Again - be aware of case: write the strings exactly as they apear in the log.

Trusted Advisor
Posts: 1,572

Re: Extract Errors and Warnings from log

You are right. This will bring one line per string found.

 

Anyway, I have the feeling that there is no way to know how many lines hold the message.

You can decide that you select 2 or more lines any time a string is found;

 

%let lines = 2;

data my_log;

     infile  "&out_dir\saslog.log" truncover;

   input a_line $200.;

   if index(a_line, 'ERROR:') > 0 or

   index(a_line, 'WARNING') > 0

   then do;

       count = &lines;

       do until (count=0);

          output;  

          count-1;

          if count > 0 then input a_line $200.;

       end;

   end;

run;

 

Contributor
Posts: 28

Re: Extract Errors and Warnings from log

That's what I expected. Thank you for the answer. We'll work with this solution for now.

If I come up with a better solution, I'll post it here. Smiley Happy

New Contributor
Posts: 2

Re: Extract Errors and Warnings from log

Posted in reply to WouterKBC

This is what I do:

 

1) Pull all Logs from Folder

2) Convert to Table

3) Look for ERROR and remove known issues

4) Combine Tables

4) Create Run Log Email

 

%LET TDATA = U:\SAS\Morning logs\ ;
%PUT &TDATA = TDATA;



filename pipedir pipe ' dir "U:\SAS\Morning logs"/s' lrecl=5000;
data indata (keep = fn date time ampm Y);
infile pipedir truncover;
input line $char1000.;
length directory $1000;
retain directory;
if line =' ' or
index(upcase(line),'<DIR>') or
left(upcase(line))=:'VOLUME' then
delete;
if left(upcase(line))=:'DIRECTORY OF' then
directory=left(substr(line,index(upcase(line),'DIRECTORY OF')+12));
if left(upcase(line))=:'DIRECTORY OF' then
delete;
date=input(substr(line,1,10),?? mmddyy10.);
TIME = Input(substr(line,12,5),??TIME.);
AMPM = Input(substr(line,18,2),  $3.);
NAME = INPUT(SUBSTR(LINE,40,3),$4.);
NAME2 = INPUT(SUBSTR(LINE,60,7),$8.);
fn = INPUT(SUBSTR(LINE,40,31),$32.);
call
symput (
'num_files'
,_n_);
call symput (
'filein'
,fn);
format date mmddyy10.;
FORMAT TIME
    TIME.;

Y=1;
    IF DATE NE TODAY() THEN DELETE;
     IF SCAN(FN,-1) NE 'log' THEN DELETE;
     if fn = 'ERROR CHECK.log' then delete;
proc sort ;
by descending DATE TIME;

run;

   
DATA X;
SET INDATA;

BY Y;
RETAIN C ;
C=C+1;
IF FIRST.Y THEN C=1;

 RUN;

data _null_;
set X;
if c = 1 then call symput('fn1',FN);
if c = 2 then call symput('fn2',FN);
if c = 3 then call symput('fn3',FN);
if c = 4 then call symput('fn4',FN);
if c = 5 then call symput('fn5',FN);
if c = 6 then call symput('fn6',FN);
if c = 7 then call symput('fn7',FN);
if c = 8 then call symput('fn8',FN);
if c = 9 then call symput('fn9',FN);
if c = 10 then call symput('fn10',FN);

if c = 11 then call symput('fn11',FN);
if c = 12 then call symput('fn12',FN);
if c = 13 then call symput('fn13',FN);
if c = 14 then call symput('fn14',FN);
if c = 15 then call symput('fn15',FN);
if c = 16 then call symput('fn16',FN);
if c = 17 then call symput('fn17',FN);
/*
if c = 18 then call symput('fn18',FN);
if c = 19 then call symput('fn19',FN);
if c = 20 then call symput('fn20',FN);
if c = 21 then call symput('fN21',FN);
*/
run;


 Filename in_e "&tdata\&fn1";
data temp;
infile in_e;
input msg $ cont $200.;
run;

data temp1;
set temp;
x= "&tdata\&fn1"  ;
if index(cont, "ERROR:" ) ge 1 then output;
run;

Results Attached

 

 

 


Run Log Check Email.JPG
SAS Super FREQ
Posts: 8,868

Re: Extract Errors and Warnings from log

Posted in reply to mstacey_foresters_com
Hi:
Check for user group papers like this one http://analytics.ncsu.edu/sesug/2008/CC-037.pdf on the topic. Writing a SAS program to read and/or summarize what's in the log is something that has been written about a lot.

cynthia
Ask a Question
Discussion stats
  • 7 replies
  • 2015 views
  • 0 likes
  • 4 in conversation