The COALESCE function is used to select the first non-missing value in a list of variables.
Sample Data
![]() |
SAS : COALESCE Function |
data temp;
input ID x1-x4;
cards;
1 . 89 85 .
2 79 . 74 .
3 80 82 86 85
;
run;
COALESCE : First Non-Missing Value
data want;
set temp;
first_non_miss = coalesce(of x1-x4);
run;
proc sql;Last Non-Missing Value
select *, coalesce(x1,x2,x3,x4) as first_non_miss
from temp;
quit;
data want;
set temp;
last_non_miss = coalesce(of x4-x1);
run;