SAS Coalescec

SAS Day 11: COALESCEC

Problem:

We want to select the non-missing value among qs1 to qs5 as QSSTRESN.

Desired Output:

 

SAS Function Background:
COALESECEC:  Return the first non-missing Character String among the input variables

Syntax:  Coalescec(” “, “a ” )=a

 

pixel2013 / Pixabay

Solution:

qsorres= strip(coalescec(qsorres1,qsorres2,qsorres3,qsorres4,qsorres5));

Alternative Solution:

Using Array

array values {*} $200 QS1 QS2 QS3 QS4 QS5;
do i =1 to dim(values);
if values{i} ne "" then QSORRES=strip(values{i});
end;

 

Mini Trick:
Coalesce return the first non-empty numeric values.
coalesce(.,1,2)=1

Note:
Coalesce does NOT work in Proc SQL sometimes, because SQL assigns a value to the variable even there is no count.
This Code will NOT work: select coalesce(count,0) as count.

Happy SAS Coding !🤪

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Social media & sharing icons powered by UltimatelySocial
%d bloggers like this: