DATA Step, Macro, Functions and more

Overwriting records from one dataset with records from another

Reply
Frequent Contributor
Posts: 138

Overwriting records from one dataset with records from another

Hi,

I have two datasets with identical variables (Dataset2 is an updated version of Dataset1). Each dataset is unique at the person level and contains ID and about 20 categorical variables. Only a subset of those in Dataset1 are in Dataset2--that is, if any person in Dataset1's information had changed, it would be reflected in Dataset2, but if their information stayed the same, they would not have a record in Dataset2:

Dataset1:

ID     categ_var1     categ_var2...etc

1              a                  d

2              a                  g

3              b                  d

4              c                  e

5              b                  f

Dataset2:

ID     categ_var1     categ_var2...etc

2             a                    b

5             c                    f

What I want is to combine the two datasets such that I have one record for each person, but if they have a record in Dataset2, SAS keeps that record instead of the one from Dataset1:

ID     categ_var1     categ_var2...etc

1              a                  d

2              a                  b

3              b                  d

4              c                  e

5              c                  f

Any help is much appreciated.

Super User
Posts: 11,343

Re: Overwriting records from one dataset with records from another

Posted in reply to Walternate

I think you may want something like:

data want;

     update dataset1 dataset2;

     by id;  /* each of the source datasets should be sorted by id if they aren't already*/

run;

Ask a Question
Discussion stats
  • 1 reply
  • 162 views
  • 0 likes
  • 2 in conversation