【问题标题】:SAS Do Loop is Omitting Rows in ProcessingSAS Do Loop 正在处理中省略行
【发布时间】:2016-11-14 15:56:26
【问题描述】:

我有以下代码。我正在尝试为关键字列表(key_words)测试一个段落(descr)。当我执行此代码时,日志会读取数组的所有变量,但只会测试 do 循环中 20,000 行中的 2 行(do i=1 到 100 等)。有关如何解决此问题的任何建议?

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;

【问题讨论】:

    标签: sas do-loops datastep


    【解决方案1】:

    你的问题是你的end=eof放错地方了。

    这是一个计算每个SASHELP.CLASS 受访者年龄值“排名”的简单示例。

    看看我把end=eof放在哪里。那是因为你需要用它来控制数组填充操作。否则,会发生什么是你的循环 do i = 1 to eof; 并没有真正按照你所说的去做:它实际上并没有在 eof 处终止,因为这永远不会是真的(因为它在 first 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;
    

    【讨论】:

    • 谢谢!如果可以的话,再做一件事。当条件满足时,我在代码第二部分中的 DO-LOOP 似乎没有停止。有什么可能发生的原因吗?
    • i=100 时不停止?还是在match=1 时不停止?后者不会阻止它,为什么会呢?
    猜你喜欢
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    相关资源
    最近更新 更多