[英]how to get last non empty column of a series from a date set in SAS
I'm working on a SAS application and I've a data set as 我正在处理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 | . | . | .
I want to get other columns and last non empty column of value as 我想获取其他列和最后一个非空列的值作为
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
code that I'm trying to do is 我想做的代码是
proc sql noprint;
create table want as
select rk, ID, name, name as value
from dim_point;
update want
set value = "";
quit;
I don't know how I can update value column with last non empty column value of value_ series? 我不知道如何使用value_系列的最后一个非空列值更新值列?
Use coalesce
in reverse order: 反向使用
coalesce
:
set value = coalesce(value_4,value_3,value_2,value_1,value_0);
You might need to use coalescec
instead for character variables. 您可能需要对字符变量使用
coalescec
。
Whilst the coalesce
solution is fine when you know all the columns, there may be situations where they can either vary, or are significantly more of them. 当您了解所有列时,虽然
coalesce
解决方案很好,但在某些情况下它们可能会有所不同,也可能会更多。
In that case, you can use an array : 在这种情况下,您可以使用数组:
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 ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.