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                            1                                  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                            1                                  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                            1                                  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;

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1915 views
  • 3 likes
  • 4 in conversation