DATA Step, Macro, Functions and more

How to Combine two variables?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 82
Accepted Solution

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.


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

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;

View solution in original post


All Replies
Super User
Posts: 10,516

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: 82

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,

Trusted Advisor
Posts: 1,204

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: 82

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: 9,682

Re: How to Combine two variables?

b1=coalescec(b1,f1);

Solution
‎10-30-2014 07:46 AM
Super User
Super User
Posts: 7,407

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: 82

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: 82

Re: How to Combine two variables?

I am asking for coalescec.

Super User
Posts: 9,682

Re: How to Combine two variables?

Yes.

coalescec is for Character , while

coalesce is for Numeric

Frequent Contributor
Posts: 82

Re: How to Combine two variables?

Hi Keshan,

Thanks.

Super Contributor
Posts: 305

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.

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

Discussion stats
  • 12 replies
  • 1093 views
  • 2 likes
  • 7 in conversation