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

## 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.

Thanks for your help.

1 ACCEPTED SOLUTION

Accepted Solutions
Diamond | Level 26

## 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;

12 REPLIES 12
Super User

## 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?

Calcite | Level 5

## 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,

Ammonite | Level 13

## Re: How to Combine two variables?

data want (drop=f1 f2);

set have;

b1=coalesce(b1,f1);

b2=coalesce(b2,f2);

run;

Calcite | Level 5

## 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,

Fluorite | Level 6

## 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

## Re: How to Combine two variables?

b1=coalescec(b1,f1);

Diamond | Level 26

## 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;

Calcite | Level 5

## 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,

Calcite | Level 5

## Re: How to Combine two variables?

I am asking for coalescec.

Super User

## Re: How to Combine two variables?

Yes.

coalescec is for Character , while

coalesce is for Numeric

Calcite | Level 5

## Re: How to Combine two variables?

Hi Keshan,

Thanks.

Barite | Level 11

## 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;

Discussion stats
• 12 replies
• 11687 views
• 3 likes
• 7 in conversation