SyntaxHighlighter

Friday, April 4, 2014

getSASLogIssues Macro

It is always a very good idea to search through your SAS log to look for any issues that may have occured.

The below macro will scan the SAS DMS log or one supplied from a proc printto log=file statement. See below example followed by the source code.

data x ;
  set sashelp.class ;
  abc = . ;
  if xyz = 1 then age = 99 ;
  weight = weight + abc ;
run ;

%getsaslogissues() ;
/**************************************************************************
*     Program: getsaslogissues.sas
*      Author: Tom Bellmer
* Responsible: Tom Bellmer
*     Created: 04Apr2014 
*     Purpose: Parse SAS log looking for issues
*       Usage: %getsaslogissues()
*       Notes: if logfile is blank then interactive SAS log is used
*              showtable only works if logfile= (blank) or interactive
*    Modified: 
**************************************************************************/

%macro getsaslogissues
  ( 
      logfile   =
    , outdsn    = saslogissues
    , showtable = y  
  ) ;

  %local 
    interactive 
    _efierr_ ;

  %let interactive = n ;   
  %let _efierr_ = 0 ;

  %if &logfile. = %then %do ;
    %let interactive = y ;
    %let logfile = %lowcase( %sysfunc( pathname( work ) ) )\temp%sysfunc( round( %sysfunc( ranuni( -1 ) ) * 800000 ) ).log ;
    dm log "file &logfile. replace" ;
  %end ;
  %else %do ;
    %if not %sysfunc( fileexist( &logfile. ) ) %then %do ;
      %put ERROR: &logfile. does not exist. ;
      %return ;
    %end ;
  %end ;

  data &outdsn. ( keep = lineno issue ) ;
    length lineno 4 ;

    infile "&logfile." 
      truncover 
      length = linelength 
      lrecl  = 32767
    ;
    input issue $varying1024. linelength ;

    if _n_ = 1 then do ;
      issue = "LOG File Location: &logfile." ;
      output ;
    end ;

    if substr( issue, 1, 4 ) in ( 'ERRO', 'WARN', 'NOTE' ) then do ;

      if substr( issue, 1, 4 ) in ( 'ERRO', 'WARN' )
        or find( issue, "converted to numeric values", 'i' ) > 0 
        or find( issue, "converted to character values", 'i' ) > 0 
        or find( issue, "new line when input", 'i' ) > 0
        or find( issue, "lost card", 'i' ) > 0
        or find( issue, "one or more lines were truncated", 'i' ) > 0
        or find( issue, "division by zero detected", 'i' ) > 0
        or find( issue, "mathematical operations could not be performed", 'i' ) > 0
        or find( issue, "missing values were generated", 'i' ) > 0 
        or find( issue, "repeats of by values", 'i' ) > 0 
        or find( issue, "cartesian", 'i' ) > 0
        or find( issue, "a case expression has no else clause", 'i' ) > 0 
        or find( issue, "identifier after a quoted string may change", 'i' ) > 0
        or find( issue, "repeats of by value", 'i' ) > 0
        or find( issue, "uninitialized", 'i' ) > 0
        or find( issue, "invalid", 'i' ) > 0
      then do ;
        lineno = _n_ ;
        output ;
      end ;

    end ;

    if _error_ then call symputx( '_efierr_', 1 ) ;
  run ;

  %if &_efierr_. = 1 %then %do ;
    data &outdsn. ;
      issue = 'problem with %getsaslogissues macro' ;
    run ;
  %end ;

  %if &interactive. = y %then %do ;
    dm "log; clear;" ;
    %if %lowcase( &showtable. ) = y %then %do ;
      dm "viewtable &outdsn. view=table" viewtable  ;
    %end ;
  %end ;
%mend ;

/* EOF: getsaslogissues.sas */