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.
PROC PRINTDATA=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!
data Inventory;
modify Inventory InventoryAdd;
by partNumber;
select (_iorc_);
when (%sysrc(_sok))do;
stock = stock + newStock;
price=newPrice;
receivedDate = today();
replace;
end;
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
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;
24
RUN;
25
PROC SORTDATA=Inventory;
26
BY partNumber;
27
RUN;
28
PROC PRINTDATA=Inventory;
29
title "Modified Inventory Data Set Sorted by partNumber";
30
RUN;
31
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.