Solved
New Contributor
Posts: 4

# New column with values from two columns: Choose value from column A if existing, otherwise choose B.

I have two columns which I want to combine into one.

It looks like this:

A                      B

0.12                 .

0.02                 0.02

0.20                 0.28

.                       .

.                       0.12

0.13                 0.15

For the new column:

In principle I want the value from the column A.

If that value is missing, I want the value from column B.

If both are missing I want ‘no sample’.

Can anyone help me with creating this new column with these values? Thank you!

Accepted Solutions
Solution
‎05-12-2016 07:48 AM
Super User
Posts: 6,934

## Re: New column with values from two columns: Choose value from column A if existing, otherwise choos

Should your new variable be character or numeric?  A numeric variable can't take on the value "no sample".  But it can take on a special missing value (such as .N) that would differentiate it from other values.  You could use:

newvar = coalesce(a, b);

if newvar=. then newvar=.N;

All Replies
Solution
‎05-12-2016 07:48 AM
Super User
Posts: 6,934

## Re: New column with values from two columns: Choose value from column A if existing, otherwise choos

Should your new variable be character or numeric?  A numeric variable can't take on the value "no sample".  But it can take on a special missing value (such as .N) that would differentiate it from other values.  You could use:

newvar = coalesce(a, b);

if newvar=. then newvar=.N;

New Contributor
Posts: 4

## Re: New column with values from two columns: Choose value from column A if existing, otherwise choos

Thank you very much for your quick reaction! It totally worked.
Super User
Posts: 9,840

## Re: New column with values from two columns: Choose value from column A if existing, otherwise choos

I agree with @Astounding that coalsece is a good idea.  I would check the logic when you have both values however, are you sure you want the first and not the min or max?  Don't know what your data is also, but in Pharma data where we follow CDISC structures, you should have a numeric and character version of these types of variables, the reason being that you can then capture all numbers in a numerical variable for processing - makes using numbers easy, and also a character representation for reporting which can contain futher information that cannot be captured in a numeric, e.g:

RESULT    RESULT_C

0.12           0.12

.                 No sample

...

E.g:

```data want;
set have;
result=coalesce(a,b);
/* Or:  result=min(a,b); */
result_c=ifc(a=. and b=.,"No sample",strip(put(min(a,b),best.)));
run;```

If your using anything other than SAS, I would avoid formats as these are SAS specific and proprietary to the software, the above two columns however are accessible to any other software.

🔒 This topic is solved and locked.