Sample 26060: MERGE data sets while conditionally overwriting common variables that are not the BY variable
Use the MERGE and BY statements to update the values of a master data set with the values of a transaction data set. Use the IN= data set option to indicate whether the transaction data set contributed to this observation. If it did not contribute, use IF-THEN logic with a DO group to preserve the original values from the master data set. You must rename variables with the RENAME= option because the master and transaction data set contain the same variables.
Note:
This is Example 3.4 from Combining and Modifying SAS Data Sets - Examples. (The original title was Applying Transactions to a Master Data Set Based on a Common Variable.)
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* Create sample data. Note MASTER has duplicates for ITEMA, so the UPDATE */
/* statement can not be used instead of MERGE. */
data master;
input itema itemb itemc;
datalines;
1 2 0
1 3 99
1 4 88
1 5 77
2 1 66
2 2 55
3 4 44
;
/* Note TRANS does not have a BY-Group for ITEMA=2. */
data trans;
input itema itemb itemc;
datalines;
1 5 6
3 3 4
;
/* Special handling is required when TRANS does not contain a matched value */
/* for ITEMA in MASTER. Use IF-THEN processing and the IN= option to */
/* determine when TRANS contributes an observation. If TRANS does not */
/* contribute, then reset the values of ITEMB and ITEMC to their original */
/* values from MASTER. */
data final(drop=oldb oldc);
merge master(rename=(itemb=oldb itemc=oldc)) trans(in=in2);
by itema;
/* If the BY-Group is not in TRANS, keep the value from MASTER */
if not in2 then
do;
itemb=oldb;
itemc=oldc;
end;
run;
proc print data=final;
title 'FINAL';
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
FINAL
Obs itema itemb itemc
1 1 5 6
2 1 5 6
3 1 5 6
4 1 5 6
5 2 1 66
6 2 2 55
7 3 3 4
Use a common variable to upate the values of variables in a master data set with the values of variables in a transaction data set without writing missing values to the revised master data set and without overlaying variable values in the program data vector.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Statements ==> File-handling ==> MERGE Common Programming Tasks ==> Combining Data SAS Reference ==> Statements ==> File-handling ==> MERGE ==> with BY Common Programming Tasks ==> Reading and Writing SAS Data
|
| Date Modified: | 2008-01-28 11:46:17 |
| Date Created: | 2006-06-16 09:29:42 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |