Sample 26061: Updating a master data set with only nonmissing values from a transaction data set
Use MERGE with a BY statement to update values in a master data set with values from a transaction data set. Use IF-THEN logic in conjunction with the RENAME= data set option to apply transaction values only if they are not missing values.
This match-merge operation requires that each data set either have an index on the BY variable or be sorted by the values of the BY variable.
Note: This is Example 4.2 from Combining and Modifying SAS Data Sets - Examples.
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. Both MASTER and TRANS contain duplicate values for */
/* the BY variable ITEM. The data sets are sorted by the values of ITEM. */
data master;
input item $ price;
format price dollar5.2;
datalines;
apple 1.99
apple 2.89
apple 1.49
grapes 1.69
grapes 2.46
orange 2.29
orange 1.89
orange 2.19
;
data trans;
input item $ price;
format price dollar5.2;
datalines;
banana 1.05
grapes 2.75
orange 1.49
orange .
orange 2.39
;
/* Combine observations from MASTER and TRANS based on the matching values */
/* for ITEM. */
data combine(drop=newprice);
/* RENAME= renames the variable PRICE in TRANS for later processing with */
/* the IF-THEN statement. */
merge master trans(rename=(price=newprice));
by item;
/* When NEWPRICE is not equal to missing, use its value to update the */
/* MASTER value of PRICE based on the current value of ITEM. If the */
/* value of NEWPRICE is missing, PRICE retains its original value from */
/* MASTER. */
if newprice ne . then price=newprice;
format price dollar5.2;
run;
proc print data=combine;
title 'Combine';
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.
Combine
Obs item price
1 apple $1.99
2 apple $2.89
3 apple $1.49
4 banana $1.05
5 grapes $2.75
6 grapes $2.75
7 orange $1.49
8 orange $1.89
9 orange $2.39
Update a master data set with values from a transaction data set, except when the transaction data set contains missing values for the variable being updated.
| 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
|
| Date Modified: | 2008-01-28 11:46:54 |
| Date Created: | 2006-06-16 12:16:42 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |