2016-11-14 56 views
2

我有以下代碼。我正在嘗試爲關鍵字列表(key_words)測試一個段落(descr)。當我執行這段代碼時,日誌會讀入數組的所有變量,但是隻會在do循環中測試20,000行中的2行(do i = 1到100)。有關如何解決此問題的任何建議?SAS Do Loop正在處理中排除

data JE.KeywordMatchTemp1; 
    set JE.JEMasterTemp end=eof; 
    if _n_ = 1 then do i = 1 by 1 until (eof); 
    set JE.KeyWords; 
    array keywords[100] $30 _temporary_; 
    keywords[i] = Key_Words; 
    end; 
    match = 0; 
    do i = 1 to 100; 
    if index(descr, keywords[i]) then match = 1; 
    end; 
    drop i; 
run; 

回答

1

您的問題是您的end=eof是在錯誤的地方。

這是一個簡單的例子,計算每個受訪者年齡值的「排名」。

看看我把end=eof。這是因爲你需要使用它來控制數組填充操作。否則,會發生什麼是你的循環是do i = 1 to eof;並沒有真正做你應該說的話:它實際上並沒有終止於eof,因爲它從來不是真的(因爲它在第一個set聲明中定義)。相反,它會因爲超出數據集的末尾而終止,這尤其是您不想要的。

這就是end=eof正在做的事情:它阻止了當數組填充數據集完成時試圖拉出一行,從而終止整個數據步驟。任何時候當你看到數據步驟在2次迭代後終止時,你可以確信這就是問題的可能性 - 這是一個非常普遍的問題。

data class_ranks; 
    set sashelp.class; *This dataset you are okay iterating over until the end of the dataset and then quitting the data step, like a normal data step.; 
    array ages[19] _temporary_; 
    if _n_=1 then do; 
    do _i = 1 by 1 until (eof); *iterate until the end of the *second* set statement; 
     set sashelp.class end=eof; *see here? This eof is telling this loop when to stop. It is okay that it is not created until after the loop is.; 
     ages[_i] = age; 
    end; 
    call sortn(of ages[*]); *ordering the ages loaded by number so they are in proper order for doing the trivial rank task; 
    end; 
    age_rank = whichn(age,of ages[*]); *determine where in the list the age falls. For a real version of this task you would have to check whether this ever happens, and if not you would have to have logic to find the nearest point or whatnot.; 
run; 
+0

謝謝!如果可以,還有一件事。看起來代碼第二部分的DO-LOOP在條件滿足時不會停止。任何可能發生的原因? –

+0

'i = 100'時不停止?或者當'match = 1'時不停止?後者不會阻止它,爲什麼呢? – Joe