MASTER TRANSACTION OBS PartNumber Quantity OBS PartNumber AddQuantity 1 1 10 1 4 14 2 2 20 2 6 16 3 3 30 3 2 12 4 4 40 5 5 50
data master; 1 set transaction; 2 modify master key=PartNumber; 3 Quantity = Quantity + AddQuantity; 4 run;
1 | Open the MASTER data set for update. |
2 | Read an observation from the TRANSACTION data set. |
3 | Match observations from the MASTER data set based on the values of PartNumber. |
4 | Update the information about Quantity by adding the new values from the TRANSACTION data set. |
ERROR: No matching observation was found in MASTER data set. PartNumber=6 AddQuantity=16 Quantity=70 _ERROR_=1 _IORC_=1230015 _N_=2 NOTE: The SAS System stopped processing this step because of errors. NOTE: The data set WORK.MASTER has been updated. There were 1 observations rewritten, 0 observations added and 0 observations deleted.
MASTER OBS PartNumber Quantity 1 1 10 2 2 20 3 3 30 4 4 54 5 5 50
data master; 1 set transaction; 2 modify master key=PartNumber; 3 select(_iorc_); 4 when(%sysrc(_sok)) do; Quantity = Quantity + AddQuantity; replace; end; when(%sysrc(_dsenom)) do; Quantity = AddQuantity; _error_ = 0; output; end; otherwise do; put 'ERROR: Unexpected value for _IORC_= ' _iorc_; put 'Program terminating. DATA step iteration # ' _n_; put _all_; stop; end; end; run;
1 | Open the MASTER data set for update. |
2 | Read an observation from the TRANSACTION data set. |
3 | Match observations from the MASTER data set based on the value of PartNumber. |
4 | Take the correct course of action based on whether a matching value for PartNumber is found in MASTER. Update Quantity by adding the new values from TRANSACTION. The SELECT group directs execution to the correct code. When a match occurs (_SOK), update Quantity and replace the original observation in MASTER. When there is no match (_DSENOM), set Quantity equal to the AddQuantity amount from TRANSACTION, and append a new observation. _ERROR_ is reset to 0 to prevent an error condition that would write the contents of the program data vector to the SAS log. When an unexpected condition occurs, write messages and the contents of the program data vector to the log, and stop the DATA step. |
MASTER ORDER OBS PartNumber Quantity OBS PartNumber 1 1 10 1 2 2 2 20 2 4 3 3 30 3 1 4 4 40 4 3 5 5 50 5 8 6 5 7 6 DESCRIPTION OBS PartNumber PartDescription 1 4 Nuts 2 3 Bolts 3 2 Screws 4 6 Washers
data combine; 1 length PartDescription $ 15; set order; 2 set description key=PartNumber; 2 set master key=PartNumber; 2 select(_iorc_); 3 when(%sysrc(_sok)) do; output; end; when(%sysrc(_dsenom)) do; PartDescription = 'No description'; _error_ = 0; output; end; otherwise do; put 'ERROR: Unexpected value for _IORC_= ' _iorc_; put 'Program terminating.'; put _all_; stop; end; end; run;
1 | Create the COMBINE data set. |
2 | Read an observation from the ORDER data set. Read an observation from the DESCRIPTION and the MASTER data sets based on a matching value for PartNumber, the key variable. Note that no error checking occurs after an observation is read from DESCRIPTION. |
3 | Take the correct course of action, based on whether a matching value for PartNumber is found in MASTER or DESCRIPTION. (This logic is based on the erroneous assumption that this SELECT group performs error checking for both of the preceding SET statements that contain the KEY= option. It actually performs error checking for only the most recent one.) The SELECT group directs execution to the correct code. When a match occurs (_SOK), the value of PartNumber in the observation that is being read from MASTER matches the current PartNumber value from ORDER. So, output an observation. When there is no match (_DSENOM), no observations in MASTER contain the current value of PartNumber, so set the value of PartDescription appropriately and output an observation. _ERROR_ is reset to 0 to prevent an error condition that would write the contents of the program data vector to the SAS log. When an unexpected condition occurs, write messages and the contents of the program data vector to the log, and stop the DATA step. |
PartNumber=1 PartDescription=Nuts Quantity=10 _ERROR_=1 _IORC_=0 _N_=3 PartNumber=5 PartDescription=No description Quantity=50 _ERROR_=1 _IORC_=0 _N_=6 NOTE: The data set WORK.COMBINE has 7 observations and 3 variables.
COMBINE OBS PartNumber PartDescription Quantity 1 2 Screws 20 2 4 Nuts 40 3 1 Nuts 10 4 3 Bolts 30 5 8 No description 30 6 5 No description 50 7 6 No description 50
data combine(drop=Foundes); 1 length PartDescription $ 15; set order; 2 Foundes = 0; 3 set description key=PartNumber; 4 select(_iorc_); 5 when(%sysrc(_sok)) do; Foundes = 1; end; when(%sysrc(_dsenom)) do; PartDescription = 'No description'; _error_ = 0; end; otherwise do; put 'ERROR: Unexpected value for _IORC_= ' _iorc_; put 'Program terminating. Data set accessed is DESCRIPTION'; put _all_; _error_ = 0; stop; end; end; set master key=PartNumber; 6 select(_iorc_); 7 when(%sysrc(_sok)) do; output; end; when(%sysrc(_dsenom)) do; if not Foundes then do; _error_ = 0; put 'WARNING: PartNumber ' PartNumber 'is not in' ' DESCRIPTION or MASTER.'; end; else do; Quantity = 0; _error_ = 0; output; end; end; otherwise do; put 'ERROR: Unexpected value for _IORC_= ' _iorc_; put 'Program terminating. Data set accessed is MASTER'; put _all_; _error_ = 0; stop; end; end; /* ends the SELECT group */ run;
1 | Create the COMBINE data set. |
2 | Read an observation from the ORDER data set. |
3 | Create the variable Foundes so that its value can be used later to indicate when a PartNumber value has a match in the DESCRIPTION data set. |
4 | Read an observation from the DESCRIPTION data set, using PartNumber as the key variable. |
5 | Take the correct course of action based on whether a matching value for PartNumber is found in DESCRIPTION. The SELECT group directs execution to the correct code based on the value of _IORC_. When a match occurs (_SOK), the value of PartNumber in the observation that is being read from DESCRIPTION matches the current value from ORDER. Foundes is set to 1 to indicate that DESCRIPTION contributed to the current observation. When there is no match (_DSENOM), no observations in DESCRIPTION contain the current value of PartNumber, so the description is set appropriately. _ERROR_ is reset to 0 to prevent an error condition that would write the contents of the program data vector to the SAS log. Any other _IORC_ value indicates that an unexpected condition has been met, so messages are written to the log and the DATA step is stopped. |
6 | Read an observation from the MASTER data set, using PartNumber as a key variable. |
7 | Take the correct course of action based on whether a matching value for PartNumber is found in MASTER. When a match is found (_SOK) between the current PartNumber value from ORDER and from MASTER, write an observation. When a match isn't found (_DSENOM) in MASTER, test the value of Foundes. If Foundes is not true, then a value was not found in DESCRIPTION either, so write a message to the log but do not write an observation. If Foundes is true, however, the value is in DESCRIPTION but not MASTER. So write an observation but set Quantity to 0. Again, if an unexpected condition occurs, write a message and stop the DATA step. |
COMBINE OBS PartNumber PartDescription Quantity 1 2 Screws 20 2 4 Nuts 40 3 1 No description 10 4 3 Bolts 30 5 5 No description 50 6 6 Washers 0