2009-11-06 40 views
2

我需要SAS來讀取許多大型日誌文件,這些日誌文件設置爲在最下方有最近的活動。我需要的只是特定活動發生的最近時間,並且我想知道SAS是否有可能跳過解析文件的(長)開始部分。SAS - 向後讀取文件?

我在線查看並發現如何向後讀取數據集,但這需要SAS首先將.log文件中的所有內容解析爲數據集。是否有可能從最後開始直接讀取文件,以便在找到特定類型的最近活動時立即停止數據步驟?

我也讀過infile和firstobs選項,但我不知道這些日誌文件在解析之前有多長,對不對?聽起來像是對我來說是一個catch-22。那麼我所描述的是可行的嗎?

回答

4

我可能會設置一個文件名管道語句,使用像tail -rtac這樣的操作系統命令以相反順序將文件呈現給SAS。這樣SAS可以正常讀取文件,而且不必擔心文件的長度。

0

如果您的意思是解析一個sas日誌文件,我不確定是否向後讀取日誌文件在實踐中是值得的。例如,下面的代碼在我的PC上執行時間不到十分之一秒,它正在寫入和讀取10,000行日誌文件。你的日誌文件有多大?有多少?同樣如下所示,您不必「解析」每一行上的所有內容。你可以有選擇地閱讀該行的一些部分,如果它不是你正在尋找的東西,那麼你可以去下一行。

%let pwd = %sysfunc(pathname(WORK)); 
%put pwd=&pwd; 
x cd &pwd; 

/* test file. more than 10,000 line log file */ 
data _null_; 
    file "test.log"; 
    do i = 1 to 1e4; 
    r = ranuni(0); 
    put r binary64.; 
    if r < 0.001 then put "NOTE: not me!"; 
    end; 
    put "NOTE: find me!"; 
    do until (r<0.1); 
    r = ranuni(0); 
    put r binary64.; 
    end; 
    stop; 
run; 

/* find the last line that starts with 
    NOTE: and get the rest of the line. */ 
data _null_; 
    length msg $80; 
    retain msg; 
    infile "test.log" lrecl=80 eof=eof truncover; 
    input head $char5. @; 
    if head = "NOTE:" then input @6 msg $char80.; 
    else input; 
    return; 
eof: 
    put "last note was on line: " _n_ ; 
    put "and msg was: " msg $80.; 
run; 
/* on log 
    last note was on line: 10013 
    and msg was: find me! 
*/