Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to assign whichever value is not missing in a list of variables to another variable in a quicker way?

Tags:

sas

I am sitting with a situation and I can achieve the desired result but I was wondering if there is perhaps a quicker way to do it.

The long way would be something like this;

         if ^missing(X) and nmiss(Y,Z) = 2 then Value = X;
    else if ^missing(Y) and nmiss(X,Z) = 2 then Value = Y;
    else if ^missing(Z) and nmiss(X,Y) = 2 then Value = Z;

This is fine for a few variables, but what happens when you have a list of many more variables. Is there a way to assign whichever value is not missing in a list of variables to another variable in a quicker way?

Something like;

    if ^missing(in(X,Y,Z)) then Value = ^missing.value;

I know that the above code is incorrect, it is just a demonstration of my thought process. Any help is greatly appreciated.

like image 933
Allan Cumming Avatar asked Jan 28 '26 11:01

Allan Cumming


2 Answers

It isn't quite what you describe, but see the coalesce function: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002518172.htm

value = coalesce(x, y, z);

This assigns the first non-missing value of x, y or z to value.

like image 131
Hong Ooi Avatar answered Jan 30 '26 13:01

Hong Ooi


To do exactly like what you say you would do:

if n(of x y z) = 1 then value = coalesce(of x y z);

Of course you'd still have to assign values for non-n=1 values.

value = ifn(n(of x y z)=1,coalesce(of x y z), mean(of x y z));

Or something like that. In that particular case the mean(of x y z) would be equivalent to coalesce(of x y z), but if your solution when multiple exist would not work with just one, then you can plug it in there.

like image 22
Joe Avatar answered Jan 30 '26 14:01

Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!