如何从SAS中设置的日期获取系列的最后一个非空列

how to get last non empty column of a series from a date set in SAS

我正在处理 SAS 应用程序,我有一个数据集

dim_point
rk | ID    | name  | value_0 | value_1 | value_2 | value_3 | value_4
1  | one   |  one  |  val_0  |  val_1  |  val_2  |  val_3  |   .
2  | two   |  two  |  val_0  |  val_1  |  val_2  |    .    |   .
3  | three | three |  val_0  |    .    |    .    |    .    |   .
4  | four  | four  |  val_0  |  val_1  |    .    |    .    |   .

我想获取其他列和最后一个非空列的值作为

want
rk | ID    | name  |  value
1  | one   |  one  |  val_3
2  | two   |  two  |  val_2
3  | three | three |  val_0
4  | four  | four  |  val_1

我尝试做的代码是

proc sql noprint;
create table want as
select rk, ID, name, name as value
from dim_point;

update want 
set value = "";
quit;

我不知道如何使用 value_ 系列的最后一个非空列值更新值列?

以相反的顺序使用coalesce

set value = coalesce(value_4,value_3,value_2,value_1,value_0);

对于字符变量,您可能需要使用 coalescec

虽然当您知道所有列时 coalesce 解决方案很好,但在某些情况下它们可能会有所不同,或者它们明显更多。

在这种情况下,您可以使用数组:

data want ;
  set have ;
  array _v{*} value_: ; /* colon operator is a wildcard, loaded in the same order as the input dataset */

  /* Loop backwards over the array, stop when we find a non-missing value */
  do i = dim(_v) to 1 by -1 until (not missing(value)) ;
    if not missing(_v{i}) then value = _v{i} ;
  end ;

  drop i ;
run ;