Search code examples
sqlsassas-macro

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

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?


Solution

  • Use coalesce in reverse order:

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

    You might need to use coalescec instead for character variables.