Published on :
Data Manipulation CREATION_INTERNE

Example: Modifying Data

This code is also available in: Deutsch Español Français
Awaiting validation
The MODIFY statement is used to update a master dataset (Inventory) based on the values contained in a transaction dataset (InventoryAdd). Observations are matched by the 'partNumber' variable. If a matching observation is found in the master, it is updated (stock and price), and the received date is refreshed. If a transaction dataset observation does not exist in the master, it is added as a new observation. The code uses the automatic _IORC_ variables and the %SYSRC macro to handle different matching scenarios.
Data Analysis

Type : CREATION_INTERNE


The examples use generated data (datalines) for the 'Inventory' and 'InventoryAdd' datasets.

1 Code Block
PROC SORT / PROC PRINT Data
Explanation :
This block creates the 'Inventory' (master) and 'InventoryAdd' (transaction) datasets with inline data. Then, it sorts both datasets by 'partNumber' and displays them to show their content before any modification. This allows for a clear comparison with the results after the MODIFY operation.
Copied!
1DATA Inventory;
2 INPUT partNumber $ partName $ stock price receivedDate :date9.;
3 FORMAT receivedDate date9.;
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;
29 
30PROC SORT DATA=Inventory; BY partNumber; RUN;
31PROC SORT DATA=InventoryAdd; BY partNumber; RUN;
32PROC PRINT DATA=Inventory; title "Inventory"; RUN;
33PROC PRINT DATA=InventoryAdd; title "InventoryAdd"; RUN;
2 Code Block
DATA STEP (MODIFY)
Explanation :
This code block uses the MODIFY statement to update the 'Inventory' dataset using information from 'InventoryAdd'.
- `by partNumber;`: Matches observations from both datasets by the 'partNumber' variable.
- `select (_iorc_);`: Uses the automatic _IORC_ variable to determine the matching result.
- `when (%sysrc(_sok))`: If a match is found (_sok), stock is increased, price is updated, received date is set to today(), and the observation is replaced (`replace;`).
- `when (%sysrc(_dsenmr))`: If no match is found (_dsenmr), a new observation is created with the new stock, new price, and today's received date, then added (`output;`). `_error_=0;` is used to prevent the DATA step from stopping due to an error.
- `otherwise`: Handles unexpected I/O errors.
After modification, the 'Inventory' dataset is sorted and displayed to show the changes.
Copied!
1DATA Inventory;
2 modify Inventory InventoryAdd;
3 BY partNumber;
4 select (_iorc_);
5 when (%sysrc(_sok))DO;
6 stock = stock + newStock;
7 price=newPrice;
8 receivedDate = today();
9 replace;
10 END;
11 when (%sysrc(_dsenmr)) DO;
12 stock=newStock;
13 price=newPrice;
14 receivedDate=today();
15 OUTPUT;
16 _error_=0;
17 END;
18 otherwise DO;
19 put "An unexpected I/O error has occurred.";
20 _error_ = 0;
21 stop;
22 END;
23 END;
24RUN;
25PROC SORT DATA=Inventory;
26 BY partNumber;
27RUN;
28PROC PRINT DATA=Inventory;
29 title "Modified Inventory Data Set Sorted by partNumber";
30RUN;
31QUIT;
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.
Copyright Info : Copyright © SAS Institute Inc. All Rights Reserved