您的問題是您的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;
來源
2016-11-14 16:16:52
Joe
謝謝!如果可以,還有一件事。看起來代碼第二部分的DO-LOOP在條件滿足時不會停止。任何可能發生的原因? –
'i = 100'時不停止?或者當'match = 1'時不停止?後者不會阻止它,爲什麼呢? – Joe