Search code examples
indexingsasproc

How do I search for a word and retrieve the whole sentence is SAS


I wrote a SAS program that outputs the SAS log to a specific folder via PROC PRINTTO. In addition, I used the INDEX function to search the log file for the string "ERROR:" . In case of an error, the INDEX function (using variable X) will be GT 1, thus activating a macro statement - sending an Email that an error occurred. I wish to find a function that looks for the string "ERROR:" but retrieves the whole sentence.


Solution

  • Not sure about sentences but it should be easy to capture the full text of the line from the log file.

    Note that SAS will write the ERROR in the first column, but the colon is not always in the sixth column, sometimes there is text before that. Just searching for 'ERROR:' in the log will miss some errors and find some lines that are not actually errors, such as SAS code used to generate error messages.

    So this code will create a dataset with all of the lines with ERROR messages on them. It will also set the macro variable NERROR to the number of errors found.

    data errors ;
      if eof then call symputx('nerror',errno);
      infile mylog truncover end=eof;
      line+1;
      input text $char200. ;
      if text=:'ERROR' and index(text,':') then do;
         errno+1;
         output;
      end;
    run;
    

    Note if you want to capture the complete error message when it takes multiple lines it will be harder. You will need to decide which of the following lines are part of the message. Plus there can be page breaks inserted in the middle of multiple line error messages.