Desktop productivity for business analysts and programmers

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

Accepted Solution Solved
Reply
New Contributor MB1
New Contributor
Posts: 4
Accepted Solution

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: 5,365

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;

View solution in original post


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

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 MB1
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
Super User
Posts: 7,720

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 327 views
  • 0 likes
  • 3 in conversation