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.

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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