The resulting data set has seven observations. Observations 2 and 3 have duplicate
values of the BY variable Common. However, the value of the variable Plant1 was not
updated in the second occurrence of the duplicate BY value.
Example 3: Using UPDATE for Processing Nonmatched
Observations, Missing Values, and New Variables
In this example, the data set Master2 is a master data set. It contains a missing value for
the variable Plant2 in the first observation, and not all of the values of the BY variable
Common are included. The transaction data set NONPlant contains a new variable
Mineral, a new value of the BY variable Common, and missing values for several
observations. The following shows the Master2 and the NONPlant input data sets:
Master2 NONPlant
OBS Common Animal2 Plant2 OBS Common Plant2 Mineral
1 a Ant 1 a Apricot Amethyst
2 c Cat Coconut 2 b Barley Beryl
3 d Dog Dewberry 3 c Cactus
4 e Eagle Eggplant 4 e
5 f Frog Fig 5 f Fennel
6 g Grape Garnet
The following program updates the data set Master2 and prints the results:
data update2_file;
update master2 nonplant;
by Common;
run;
proc print data=update2_file;
title 'Data Set Update2_File';
run;
Output 21.16 Results of Updating with New Variables, Nonmatched Observations, and
Missing Values
Combining SAS Data Sets: Methods 503
As shown, all observations now include values for the variable Mineral. The value of
Mineral is set to missing for some observations. Observations 2 and 6 in the transaction
data set did not have corresponding observations in Master2, and they have become new
observations. Observation 3 from the master data set was written to the new data set
without change, and the value for Plant2 in observation 4 was not changed to missing.
Three observations in the new data set have updated values for the variable Plant2.
The following program uses the UPDATEMODE statement option in the UPDATE
statement, and prints the results:
data update2_file;
update master2 nonplant updatemode=nomissingcheck;
by Common;
run;
proc print data=update2_file;
title 'Data Set Update2_File - UPDATEMODE Option';
run;
Output 21.17 Results of Updating with the UPDATEMODE Option
The value of Plant2 in observation 5 is set to missing because the
UPDATEMODE=NOMISSINGCHECK option is in effect.
For detailed examples for updating data sets, see Combining and Modifying SAS Data
Sets: Examples.
Example 4: Updating a Master Data Set By Adding Observations
If the transaction data set contains observations that do not match observations in the
master data set, you must alter the program. The following example uses the MODIFY
statement to update the master data set, Inventory, with values from the transaction data
set, Add_Inventory. The following shows the Inventory and the Add_Inventory input
data sets.
504 Chapter 21 Reading, Combining, and Modifying SAS Data Sets
Master
Part In Received
Number Description Stock Date Price
K89R seal 34 27jul1998 245.00
M4J7 sander 98 20jun1998 45.88
LK43 filter 121 19may1999 10.99
MN21 brace 43 10aug1999 27.87
BC85 clamp 80 16aug1999 9.55
NCF3 valve 198 20mar1999 24.50
KJ66 cutter 6 18jun1999 19.77
UYN7 rod 211 09sep1999 11.55
JD03 switch 383 09jan2000 13.99
BV1E timer 26 03aug2000 34.50
Add_Inventory
Part In
Number Description Stock Price
K89R seal 6 247.50
AA11 hammer 55 32.26
BB22 wrench 21 17.35
KJ66 cutter 10 24.50
CC33 socket 7 22.19
BV1E timer 30 36.50
Notice that observation 2 in the input data set Add_Inventory has no match in the master
data set, Inventory. Therefore, you must use an explicit OUTPUT statement to write a
new observation to the master data set.
1
Once you specify an explicit OUTPUT statement, you must also specify a REPLACE
statement to update those observations that are in both data sets.
The following program uses the MODIFY, UPDATE, and REPLACE statements to add
new observations to the master data set and update the existing observations.
Example Code 21.1 Updating a Master Data Set By Adding Observations
data INVENTORY;
modify Inventory Add_Iventory; 1
by PartNumber;
select (_iorc_); 2
when (%sysrc(_sok)) 3
do;
InStock=InStock+NewStock;
ReceivedDate=today();
Price=NewPrice;
replace; 4
1
The OUTPUT statement is required because the default action for a DATA step using a MODIFY statement is REPLACE, not
OUTPUT.
Combining SAS Data Sets: Methods 505
end;
when (%sysrc(_dsenmr)) 5
do;
InStock=NewStock;
ReceivedDate=today();
Price=NewPrice;
output; 6
_error_=0;
end;
otherwise 7
do;
put 'An unexpected I/O error has occurred.'
_error_=0;
stop;
end;
end;
run;
proc print data=INVENTORY;
title 'Data Set INVENTORY (Updated)';
run;
quit;
1
Use the MODIFY statement to load the data from the data sets Inventory and
Add_Inventory.
2
Use the SELECT statement to process the group of statements conditionally.
3
Use the SYSRC autocall macro with the automatic variable, _IORC_, to check for
errors. If the value of _IORC_ is _SOK, then an observation in the transaction data
set matches an observation in the master data set.
4
Use the REPLACE statement to update the data set Inventory using observations
from the transaction data set.
5
Use the SYSRC autocall macro with the automatic variable, _IORC_, to check for
errors. If the value of _IORC_ is _DSENMR, then an observation in the transaction
data set does not exist in the master data set.
6
Use the OUTPUT statement to write the current observation to the end of the master
data set.
7
If neither condition is met, use the PUT statement to write a message to the log.
506 Chapter 21 Reading, Combining, and Modifying SAS Data Sets
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.230.81