## How to Combine two variables?

Solved
Frequent Contributor
Posts: 87

# How to Combine two variables?

Dear All,

Here is my data file:

ID type b1 b2 f1 f2

1   a      5  6

1   b               1   2

1   c               2   5

2   a      4  3

2   b              6   6

2   c              3   5

Variable b1 and b2 are baseline variables.

f1 and f2 are follow up variables. b1 and f1 are same.

b2 and f2 are same. Is there a way to combine these 2 same variables into 1 variable.

Accepted Solutions
Solution
‎10-30-2014 07:46 AM
Super User
Posts: 9,599

## Re: How to Combine two variables?

Just for a completely different way, set the data together with a rename:

data want;
set have (drop=f1 f2 rename=(b1=f1 b2=f2) where=(type="a"))
have (where=(type ne "a") drop=b1 b2);
run;

All Replies
Super User
Posts: 13,583

## Re: How to Combine two variables?

Lots of ways but whether they're what you want ...

Provide an example of what the final output should look like for your example data.

You say b1 and f1, and b2 and f2, are the same but I don't see any that show that behavior in your example.

I see b1=5 and f2=5 but none with b1=f1.

And what role do ID and Type play in this request?

Frequent Contributor
Posts: 87

## Re: How to Combine two variables?

Hi Ballardw,

Thanks for helping me. b1 and fi are same king of variable, b2 and f2 also same kind of variable. Type is like time variable, type=a means baseline test. type= b or c mean follow up test. ID is to identify subject. I want know difference between baseline and follow up. the final data file should like this:

ID type b1 b2

1   a     5  6

1   b    1   2

1   c    2   5

2   a    4  3

2   b    6   6

2   c    3   5

Thanks,

Posts: 1,270

## Re: How to Combine two variables?

data want (drop=f1 f2);

set have;

b1=coalesce(b1,f1);

b2=coalesce(b2,f2);

run;

Frequent Contributor
Posts: 87

## Re: How to Combine two variables?

Hi,

I use coalesce function to combine numeric variables done well. doesn't work for char variables. log file showed 'NOTE': invalid numeric data,. for that char variable. Is there a way to combine char variables?

Thanks so much,

Contributor
Posts: 45

## Re: How to Combine two variables?

You can also simply use this code for char variables......

DATA WANT(DROP=F1 F2);

SET HAVE;

IF B1 EQ ' ' AND F1 NE ' ' THEN B1 = F1;

IF B2 EQ ' ' AND F2 NE ' ' THEN B2 = F2;

RUN;

Super User
Posts: 10,787

## Re: How to Combine two variables?

b1=coalescec(b1,f1);

Solution
‎10-30-2014 07:46 AM
Super User
Posts: 9,599

## Re: How to Combine two variables?

Just for a completely different way, set the data together with a rename:

data want;
set have (drop=f1 f2 rename=(b1=f1 b2=f2) where=(type="a"))
have (where=(type ne "a") drop=b1 b2);
run;

Frequent Contributor
Posts: 87

## Re: How to Combine two variables?

RW9 and Keshan,

Thanks for your help. BOTH WAYS work well.

Just ask Keshan, coalescec function only works for char variables?

Thanks,

Frequent Contributor
Posts: 87

Super User
Posts: 10,787

## Re: How to Combine two variables?

Yes.

coalescec is for Character , while

coalesce is for Numeric

Frequent Contributor
Posts: 87

## Re: How to Combine two variables?

Hi Keshan,

Thanks.

Super Contributor
Posts: 319

## Re: How to Combine two variables?

Hello,

Assuming id and type are unique :

data have;
infile datalines  truncover;
input ID type \$ b1  b2  f1 f2;
datalines;
1   a 5  6
1   b . . 1   2
1   c . . 2   5
2   a 4  3
2   b . . 6   6
2   c . . 3   5
;

proc sort data=have;

by id type;

run;

data want;
update have have(drop=b1 b2 rename=(f1=b1 f2=b2));
by id type;

drop f1 f2;
run;

🔒 This topic is solved and locked.