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


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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

12 REPLIES 12
ballardw
Super User

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?

xinjian
Calcite | Level 5

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,

stat_sas
Ammonite | Level 13

data want (drop=f1 f2);

set have;

b1=coalesce(b1,f1);

b2=coalesce(b2,f2);

run;

xinjian
Calcite | Level 5


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,

RaviKommuri
Fluorite | Level 6

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;

Ksharp
Super User

b1=coalescec(b1,f1);

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

xinjian
Calcite | Level 5

RW9 and Keshan,

Thanks for your help. BOTH WAYS work well.

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

Thanks,

xinjian
Calcite | Level 5

I am asking for coalescec.

Ksharp
Super User

Yes.

coalescec is for Character , while

coalesce is for Numeric

xinjian
Calcite | Level 5

Hi Keshan,

Thanks.

Loko
Barite | Level 11

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 12 replies
  • 11687 views
  • 3 likes
  • 7 in conversation