DATA Step, Macro, Functions and more

Updating a master data set from an update data set.

Reply
N/A
Posts: 0

Updating a master data set from an update data set.

The master data set MDS have variables ID, X and several others. The update data set UDS have variables ID and X.

New values for X is given in UDS.

If there were only one observation for each ID, I could use the code:

UPDATE MDS UDS;
BY ID;

But how do I code if there are multiple observations for each ID in the master data set, and I want all observations for an ID(identity) updated according to the update data set? Message was edited by: ErnestoC
Super Contributor
Super Contributor
Posts: 3,174

Re: Updating a master data set from an update data set.

Performed a search of the SAS support websitte http://support.sas.com/ with the two SAS-hosted documentation references for your consideration. The site has a wealth of supplemental technical reference documents as well.

Scott Barry
SBBWorks, Inc.

Updating SAS Data Sets
http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001304308.htm

Modifying SAS Data Sets
http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001331353.htm
N/A
Posts: 0

Re: Updating a master data set from an update data set.

Before I put my question on this forum I have been searching on the Internet, but haven't found any solution.

I state that UPDATE doesn't work. I have tried with MERGE and MODIFY in different ways, but the only X values that are changed are the ones for each ID:s first record.

I hope someone can help me. Many people must have had to do the same kind of operation.
Super Contributor
Super Contributor
Posts: 3,174

Re: Updating a master data set from an update data set.

Share your SAS code with the SAS log - post a reply pasting the information. Also, you will want to include PUTLOG _ALL_; messages (dump all variables to SAS log). Of course this information may help with your own problem debugging before sending to the forum. SAS version (and service pack), operating platform are also useful. Of course, after exhausting all available debugging options, you may want to contact SAS support with your particulars, opening a SAS track on the website.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: Updating a master data set from an update data set.

Scott Barry, are you God?
N/A
Posts: 0

Re: Updating a master data set from an update data set.

Oh God, I did it just by myself.

DATA UDS(RENAME=(X=Y));
SET UDS;
RUN;

DATA MDS;
MERGE MDS UDS;
BY ID;
RUN;

DATA MDS(DROP=Y);
SET MDS;
IF X~=Y AND Y~=. THEN X=Y;
RUN;

Good heavens!
Super Contributor
Super Contributor
Posts: 3,174

Re: Updating a master data set from an update data set.

Glad to see you were successful - for optimization, you may be able to reduce your code to a single DATA step, doing the RENAME= and DROP= on the SET statement (data set options in parentheses), and along with using the IN= on the SET for each contributing SAS file with your MERGE.

Scott Barry
SBBWorks, Inc.
Frequent Contributor
Posts: 127

Re: Updating a master data set from an update data set.

Hi,

I think that the following solution should also meet your expectations.
The advantage is that your datasets do not need to be sorted in the same order and/or BY-variables.

Please let me know Smiley Wink

Regards,
Florent

PROC SQL;
update MDS a
set X = (select coalesce(b.X, a.X)
from UDS b
where a.ID = b.ID);
QUIT;
Ask a Question
Discussion stats
  • 7 replies
  • 179 views
  • 0 likes
  • 3 in conversation