Published on :
ETL CREATION_INTERNE

Example: Modifying Data in a SAS Data Set

This code is also available in: Deutsch Español Français
Awaiting validation
The documentation explains the use of the `MODIFY` statement to manage inventory updates. It details how the `DATA Step` interacts with the `Inventory` (master) and `InventoryAdd` (transactions) data sets. By using the automatic variable `_IORC_` with the `%SYSRC` macro, the code distinguishes existing observations (`_SOK`) from new ones (`_DSENMR`). For existing observations, the `REPLACE` statement updates stock quantities and prices. For new observations, the `OUTPUT` statement adds them to the master data set. The flexibility of `MODIFY` is highlighted for its ability to modify a data set without creating a new one, while requiring explicit management of actions (REPLACE or OUTPUT) when these statements are used.
Data Analysis

Type : CREATION_INTERNE


The examples use generated data (datalines) to create the 'Inventory' and 'InventoryAdd' data sets.

1 Code Block
DATA Step Data
Explanation :
This code block creates the two data sets required for the example:
- `Inventory` : The master data set representing the current inventory of a warehouse. It contains information about part number, name, stock, price, and received date.
- `InventoryAdd` : The transaction data set containing updates. It includes new parts to add, stock adjustments (`newStock`), and price changes (`newPrice`) for existing or new items. Dates are read with a specific informat.
Copied!
1DATA Inventory;
2 informat receivedDate date9.;
3 INPUT partNumber $ partName $ stock price receivedDate;
4 DATALINES;
5K89R seal 34 245.00 07jul2015
6M4J7 sander 98 45.88 20jun2015
7LK43 filter 121 10.99 19may2016
8MN21 brace 43 27.87 10aug2016
9BC85 clamp 80 9.55 16aug2016
10NCF3 valve 198 24.50 20mar2016
11KJ66 cutter 6 19.77 18jun2016
12UYN7 rod 211 11.55 09sep2016
13JD03 switch 383 13.99 09jan2017
14BV1E timer 26 34.50 03aug2017
15;
16RUN;
17 
18DATA InventoryAdd;
19 INPUT partNumber $ partName $ newStock newPrice;
20 DATALINES;
21AA11 hammer 55 32.26
22BB22 wrench 21 17.35
23BV1E timer 30 36.50
24CC33 socket 7 22.19
25K89R seal 6 247.50
26KJ66 cutter 10 24.50
27;
28RUN;
2 Code Block
PROC SORT / PROC PRINT
Explanation :
These steps sort both data sets by `partNumber` to facilitate matching and visualizing changes. `PROC PRINT` is then used to display the initial content of the `Inventory` and `InventoryAdd` data sets before any modification, allowing a clear comparison of the results after the `MODIFY` operation.
Copied!
1PROC SORT DATA=Inventory; BY partNumber; RUN;
2PROC SORT DATA=InventoryAdd; BY partNumber; RUN;
3PROC PRINT DATA=Inventory; title "Inventory"; RUN;
4PROC PRINT DATA=InventoryAdd; title "InventoryAdd"; RUN;
3 Code Block
DATA Step (MODIFY)
Explanation :
This code block uses the `MODIFY` statement to update the `Inventory` data set based on information from the `InventoryAdd` data set. The `BY partNumber` statement ensures observations are matched. The `SELECT (_IORC_)` clause handles two scenarios:
1. If a matching observation is found (`%SYSRC(_SOK)`), the `stock`, `price`, and `receivedDate` values are updated, then `REPLACE` writes the modified observation. The `receivedDate` is updated because even if it's an existing item, new stock has been received.
2. If no matching observation is found (`%SYSRC(_DSENMR)`), a new observation is created with values from `InventoryAdd`, and `OUTPUT` adds it to the `Inventory` data set. `_ERROR_=0` is used to suppress error messages from the log for new observations.
An `OTHERWISE` block is included to handle unexpected I/O errors. Finally, the `Inventory` data set is sorted and displayed to show the changes.
Copied!
1DATA Inventory;
2 modify Inventory InventoryAdd;
3 BY partNumber;
4 select (_iorc_);
5 /*** The observation exists in the master data set */
6 when (%sysrc(_sok))DO;
7 stock = stock + newStock;
8 price=newPrice;
9 receivedDate = today();
10 replace;
11 END;
12 /*** The observation does not exist in the master data set*/
13 when (%sysrc(_dsenmr)) DO;
14 stock=newStock;
15 price=newPrice;
16 receivedDate=today();
17 OUTPUT;
18 _error_=0;
19 END;
20 otherwise DO;
21 put "An unexpected I/O error has occurred."
22 _error_ = 0;
23 stop;
24 END;
25 END;
26RUN;
27PROC SORT DATA=Inventory;
28 BY partNumber;
29RUN;
30PROC PRINT DATA=Inventory;
31 title "Modified Inventory Data Set Sorted by partNumber";
32RUN;
33QUIT;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.