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.
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;
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?
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,
data want (drop=f1 f2);
set have;
b1=coalesce(b1,f1);
b2=coalesce(b2,f2);
run;
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,
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;
b1=coalescec(b1,f1);
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;
RW9 and Keshan,
Thanks for your help. BOTH WAYS work well.
Just ask Keshan, coalescec function only works for char variables?
Thanks,
I am asking for coalescec.
Yes.
coalescec is for Character , while
coalesce is for Numeric
Hi Keshan,
Thanks.
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.