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.
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!
proc sort data=Inventory; by partNumber; run;
proc sort data=InventoryAdd; by partNumber; run;
proc print data=Inventory; title "Inventory"; run;
proc print data=InventoryAdd; title "InventoryAdd"; run;
1
PROC SORTDATA=Inventory; BY partNumber; RUN;
2
PROC SORTDATA=InventoryAdd; BY partNumber; RUN;
3
PROC PRINTDATA=Inventory; title "Inventory"; RUN;
4
PROC PRINTDATA=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!
data Inventory;
modify Inventory InventoryAdd;
by partNumber;
select (_iorc_);
/*** The observation exists in the master data set */
when (%sysrc(_sok))do;
stock = stock + newStock;
price=newPrice;
receivedDate = today();
replace;
end;
/*** The observation does not exist in the master data set*/
when (%sysrc(_dsenmr)) do;
stock=newStock;
price=newPrice;
receivedDate=today();
output;
_error_=0;
end;
otherwise do;
put "An unexpected I/O error has occurred."
_error_ = 0;
stop;
end;
end;
run;
proc sort data=Inventory;
by partNumber;
run;
proc print data=Inventory;
title "Modified Inventory Data Set Sorted by partNumber";
run;
quit;
1
DATA 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;
26
RUN;
27
PROC SORTDATA=Inventory;
28
BY partNumber;
29
RUN;
30
PROC PRINTDATA=Inventory;
31
title "Modified Inventory Data Set Sorted by partNumber";
32
RUN;
33
QUIT;
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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.