Sample 24699: Updating a master data set by accumulating duplicate transactions
Update a master data set in place using values supplied by
a second data set containing duplicates for the key value.
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 set with index on ITEM */
data veggies(index=(item));
input Item $ Inventory ;
datalines;
Corn 12
Lettuce 6
Potato 15
Onion 7
;
/* Create transaction data set */
data new_inventory;
input Item $ Quantity;
datalines;
Lettuce 2
Onion 7
Lettuce 3
;
/* There is no automatic update of like named variables with KEY=. */
/* If the goal is to accumulate the values of INVENTORY instead of */
/* overwriting them, an accumulation statement is needed. */
data veggies;
set new_inventory;
modify veggies key=Item/unique;
select (_iorc_);
when (%sysrc(_sok)) do;
/* To accumulate instead of overwrite */
inventory=inventory + quantity ;
replace;
end;
when (%sysrc(_dsenom)) do;
_ERROR_=0;
inventory=.;
put 'no match' inventory=;
put "Item not in stock --- " Item;
end;
otherwise do;
put 'Unexpected ERROR: _iorc_= ' _iorc_;
stop;
end;
end;
run;
proc print data=veggies;
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.
Obs Item Inventory
1 Corn 12
2 Lettuce 11
3 Potato 15
4 Onion 14
Update a master data set in place using values supplied by
a second data set containing duplicates for the key value.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Statements ==> File-handling ==> MODIFY Common Programming Tasks ==> Combining Data SAS Reference ==> Statements ==> File-handling ==> MODIFY ==> with KEY=
|
| Date Modified: | 2008-01-25 14:38:31 |
| Date Created: | 2004-09-30 14:09:05 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |