Hi everyone.
I have two large data sets.
Data1 is my main data set with missing information. I need to get that information in Data2 to fill the gap in Data1.
Data1
Name Admission_date Smoking_Status Weight
Maria 22Nov16 1 120
Maria 10Dec20 2
Luna 02Jan15 85
Rey 18Mar19 2 100
Rey 05May21 3 105
Jim 13Sep14
Data2
Name Admission_date Smoking_Status Weight
Maria 10Dec20 2 110
Luna 02Jan15 3 85
Rey 18Mar19 2 100
Juan 12Aug21 1 75
Jim 13Sep14 1 na
So my final data set would like the one below.
Final data
Name Admission_date Smoking_Status Weight
Maria 22Nov16 1 120
Maria 10Dec20 2 110
Luna 02Jan15 3 85
Rey 18Mar19 2 100
Rey 05May21 3 105
Jim 13Sep14 1 na
Thank you very much in advance for your help!
Assuming data is sorted by Name and ID and you can merge by those.
data want;
merge data1 (rename = (smoking_status = smoke1 weight = weight1))
data2 (rename = (smoking_status = smoke2 weight = weight2));
by name admission_date;
smoking_status = coalesce(smoke1, smoke2);
weight = coalesce(weight1, weight2);
run;
@yoyong555 wrote:
Hi everyone.
I have two large data sets.
Data1 is my main data set with missing information. I need to get that information in Data2 to fill the gap in Data1.
Data1
Name Admission_date Smoking_Status Weight
Maria 22Nov16 1 120
Maria 10Dec20 2
Luna 02Jan15 85
Rey 18Mar19 2 100
Rey 05May21 3 105
Jim 13Sep14
Data2
Name Admission_date Smoking_Status Weight
Maria 10Dec20 2 110
Luna 02Jan15 3 85
Rey 18Mar19 2 100
Juan 12Aug21 1 75
Jim 13Sep14 1 na
So my final data set would like the one below.
Final data
Name Admission_date Smoking_Status Weight
Maria 22Nov16 1 120
Maria 10Dec20 2 110
Luna 02Jan15 3 85
Rey 18Mar19 2 100
Rey 05May21 3 105
Jim 13Sep14 1 na
Thank you very much in advance for your help!
Assuming data is sorted by Name and ID and you can merge by those.
data want;
merge data1 (rename = (smoking_status = smoke1 weight = weight1))
data2 (rename = (smoking_status = smoke2 weight = weight2));
by name admission_date;
smoking_status = coalesce(smoke1, smoke2);
weight = coalesce(weight1, weight2);
run;
@yoyong555 wrote:
Hi everyone.
I have two large data sets.
Data1 is my main data set with missing information. I need to get that information in Data2 to fill the gap in Data1.
Data1
Name Admission_date Smoking_Status Weight
Maria 22Nov16 1 120
Maria 10Dec20 2
Luna 02Jan15 85
Rey 18Mar19 2 100
Rey 05May21 3 105
Jim 13Sep14
Data2
Name Admission_date Smoking_Status Weight
Maria 10Dec20 2 110
Luna 02Jan15 3 85
Rey 18Mar19 2 100
Juan 12Aug21 1 75
Jim 13Sep14 1 na
So my final data set would like the one below.
Final data
Name Admission_date Smoking_Status Weight
Maria 22Nov16 1 120
Maria 10Dec20 2 110
Luna 02Jan15 3 85
Rey 18Mar19 2 100
Rey 05May21 3 105
Jim 13Sep14 1 na
Thank you very much in advance for your help!
data want;
update data1
data2;
by name admission_date;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.