BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MB1
Calcite | Level 5 MB1
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

3 REPLIES 3
Astounding
PROC Star

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;

MB1
Calcite | Level 5 MB1
Calcite | Level 5
Thank you very much for your quick reaction! It totally worked.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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