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

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                                                             na 

 

Thank you very much in advance for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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                                                             na 

 

Thank you very much in advance for your help!


 

View solution in original post

3 REPLIES 3
Reeza
Super User

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                                                             na 

 

Thank you very much in advance for your help!


 

Astounding
PROC Star
Two questions are critical to providing the best answer.

What should happen when both data sets contain a nonmissing value for WEIGHT, but the values are different?

Should Juan, who appears only in data set 2, really be omitted from the final result?
Ksharp
Super User
data want;
update data1
       data2;
by name admission_date;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 401 views
  • 3 likes
  • 4 in conversation