在当前观察中读取下一个非缺失观察值

Reading next non-missiong observation's value in current observation

我的问题与 reading next observation's value in current observation 非常相似,只是我想阅读下一个 Non-Missing 观察(在 SAS 中)。

例如,这是我的数据集

data have;
   input ID Salary;
   cards;
10 1000
20 .
30 3000
40 .
50 .
60 6000
;
run;

我正在寻找这样的东西

ID Salary
10 1000
20 3000
30 3000
40 6000
50 6000
60 6000

如您所见,对于 ID 40,薪水从下一个非缺失观察值中获取值,即来自 ID 60、6000。

链接的问题给出了几个建议,其中大部分可以适应这个。

例如,接受的答案可以稍微修改。

data want;
  set have;
  if missing(salary) then
    do _i = _n_+1 to nobs;
      set have(keep=salary) nobs=nobs point=_i;
      if not missing(salary) then leave;
    end;
run;

您也可以对数据进行反向排序并使用保留来完成相同的操作。如果你有很多缺失,那可能会更有效率。