SAS編程-宏:SAS日志文件中相關(guān)Issue的檢查

SAS程序運行之后,為了確保程序運行成功,需要檢查SAS的日志文件。檢查的本質(zhì)是,關(guān)鍵詞篩查——輸出含有不被接受的關(guān)鍵詞的日志記錄。

對于此,不同的公司可能有不同的處理方式。目前,我們公司是用腳本進行處理。我覺得腳本運行、輸出界面不夠友好,于是使用SAS進行實現(xiàn)。完整宏程序在第2節(jié)匯總。

1. 思路介紹

日志檢查分為兩部分。第一部分,將日志文件內(nèi)容讀入SAS數(shù)據(jù)集;第二部分,輸出含有特定關(guān)鍵詞的日志記錄。

第一部分,參考SAS編程:如何批量讀入某路徑下外部文檔數(shù)據(jù)?

第二部分,關(guān)于日志文件的關(guān)鍵詞,各個公司應(yīng)該大同小異。舉例如下:

ERROR
WARNING
_ERROR_
already on the library
already sorted
appears on a DELETE
At least
Cartesian product joins
ERROR DETECTED
has no effect
ignored
will be overwritten
Invalid
is not valid
is invalid
ivision by zero
Mathematical
Message
Missing values were gen
misspelled
more than one data set with repeats of BY values
nreferenced label
outside the axis range
pparent
requires remerging
roups are not created
stopped due to looping
syntax error
The query as specified involves
uninitialized
values have been converted
went to a new line
INTERRUPTION

將兩部分內(nèi)容結(jié)合,就是組成檢查SAS日志相關(guān)issue的宏程序了。

2. 具體宏程序

考慮到Windows和UNIX系統(tǒng)中,文件地址的斜杠不同,以及在輸入文件夾地址時,地址末尾可能添加斜杠,也可能不添加。在宏程序中,根據(jù)輸入的地址判斷斜杠的類型,同時,統(tǒng)一將輸入地址末尾的斜杠移除。

%macro check_log(outdt=, dirpath=);
**Author: Jihai;
**Date: 2022-06-01;

%if  "&dirpath." ne "" %then %do;

%local dirpath_tmp slash;

**Get the slash;
%let slash = %substr(%sysfunc(compress(&dirpath., : _ , a d)), 1, 1);

**Remove trailing slash;
%if "%substr(&dirpath.,%length(&dirpath.),1)" = "&slash." %then %let dirpath_tmp=%substr(&dirpath.,1,%length(&dirpath.)-1);
%else %let  dirpath_tmp = &dirpath.;


**Get filepath;
data _tmp1;
  fileres = filename("dirpath", "&dirpath_tmp.");
  dirid = dopen("dirpath");
  num = dnum(dirid);

  length direct filename filepath $200;

  if dirid > 0 and num >0 then do;
    do i = 1 to num;
      direct = "&dirpath_tmp.";
      filename = dread(dirid, i);
      filepath = catx("&slash.", direct, filename);

      if upcase(scan(filename, -1, ".")) in ("LOG")  then output; 
    end;
  end;

  keep filename filepath;

  proc sort;
    by filename;
run;

**Output Issue records;
data &outdt.;
    set _tmp1;
    infile dummy  filevar=filepath end=lastrec truncover;

    do while(not lastrec);
        input text $1000.;
        if index(text, "ERROR") or  index(text, "WARNING") or  index(text, "_ERROR_") or  index(text, "already on the library") or  index(text, "already sorted") or  index(text, "appears on a DELETE") or  index(text, "At least") or 
            index(text, "Cartesian product joins") or  index(text, "ERROR DETECTED") or  index(text, "has no effect") or  index(text, "ignored") or  index(text, "will be overwritten") or  index(text, "Invalid") or  index(text, "is not valid") or   index(text, "is invalid") or 
            index(text, "ivision by zero") or  index(text, "Message") or index(text, "Missing values were gen") or  index(text, "Mathematical") or  index(text, "misspelled") or  index(text, "more than one data set with repeats of BY values") or  index(text, "nreferenced label") or 
            index(text, "outside the axis range") or  index(text, "pparent") or  index(text, "requires remerging") or  index(text, "roups are not created") or  index(text, "stopped due to looping") or 
            index(text, "syntax error") or  index(text, "The query as specified involves") or  index(text, "uninitialized") or  index(text, "values have been converted") or  index(text, "went to a new line") or 
            index(text, "INTERRUPTION")
                then  output;   
    end;
run;
%end;

%else %put Dirpath is missing ! ;

%mend check_log;

***Invoke the macro;
%check_log(
    outdt= check_log
    ,dirpath= E:\99_Test\Test\test5\
);

有一點需要注意,這個宏程序的記錄篩選的關(guān)鍵詞是基于英文環(huán)境下的,如果具體issue以中文的形式展示,就無法被篩選出來了。

輸出結(jié)果如下:

Result

總結(jié)

宏程序的主要難點在于第一部分——外部文件的讀入,篩選記錄的環(huán)節(jié)比較簡單直白。如想要增減關(guān)鍵詞,可以直接在if條件語句中修改。

感謝閱讀, 歡迎關(guān)注!
若有疑問,歡迎評論交流!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容