我有以下代码。我试图测试一个段落(descr)的关键字列表(key_words)。当我执行此代码时,日志会读入数组的所有变量,但只会测试do循环中20,000行中的2行(do i = 1到100和on)。有关如何解决此问题的任何建议?
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;
答案 0 :(得分:1)
您的问题是您的end=eof
位置错误。
这是计算每个SASHELP.CLASS
受访者年龄值“等级”的简单示例。
查看我放置end=eof
的位置。那是因为您需要使用它来控制阵列填充操作。否则,你的循环do i = 1 to eof;
实际上并没有真正做到你应该说的那样:它实际上并没有终止于eof
,因为它永远不会是真的(因为它在< em>第一次 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;