![]() | ![]() | ![]() | ![]() | ![]() |
Update an indexed data set in place using the KEY= option to locate the matching observations.
Note: When the master data set contains duplicate values, only the first matching observation of the KEY value is updated. See Example 1.
To force a continuous search of the master index file, use a DO loop until a non-match condition is encountered. See Example 2.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
/*****************************************************************************/
/* An index is created for data set STORE (master) using the INDEX= data set */
/* option for the key variable ITEM. */
/* */
/* An observation is read from the GROCERY_LIST (transaction) data set and */
/* and a matching observation in STORE is located using the index. The */
/* automatic variable _IORC_ is tested using the mnemonics defined in the */
/* SAS-supplied %SYSRC autocall macro. The value _SOK means a match was */
/* found, and the value _DSENOM means a match was not found. */
/*****************************************************************************/
/* Example 1: Only first matching observation updated */
/* due to duplicates in master */
data Dairy(index=(Item));
input Item $ Inventory Price ;
datalines;
Milk 15 1.99
Milk 3 1.99
Soymilk 8 1.79
Eggs 24 1.29
Cheese 14 2.19
;
data Dairy_current_price;
input Item $ price_increase;
datalines;
Milk .05
Butter .10
;
data Dairy;
set Dairy_current_price;
modify Dairy key=Item;
select (_iorc_);
when (%sysrc(_sok)) do;
/* calculate new price for match, and replace observation */
Price=Price + Price_increase;
replace;
end;
when (%sysrc(_dsenom)) do;
/* When item is not in master, reset _ERROR_ flag */
/* and output note to log. */
_ERROR_=0;
put "Item not in stock --- " Item;
end;
otherwise do; /* Unplanned condition, stop the data step */
put 'Unexpected ERROR: _iorc_= ' _iorc_;
stop;
end;
end;
run;
proc print data=dairy;
run;
/* Example 2: Forcing updates to occur on all duplicates in master */
/* Use DO loop to apply single transaction to multiple consecutive */
/* duplicates in master data set. */
data Dairy(index=(Item));
input Item $ Inventory Price ;
datalines;
Milk 15 1.99
Milk 3 1.99
Soymilk 8 1.79
Eggs 24 1.29
Cheese 14 2.19
;
data Dairy_current_price;
input Item $ price_increase;
datalines;
Milk .05
Butter .10
;
data Dairy;
set Dairy_current_price;
master_count = 0;
do until (_IORC_=%SYSRC(_DSENOM));
modify Dairy key=Item;
select (_iorc_);
when (%sysrc(_sok)) do;
Price=Price + Price_increase;
master_count + 1;
replace;
end;
when (%sysrc(_dsenom)) do;
_ERROR_=0;
if master_count = 0 then put "Item not in stock --- " Item;
end;
otherwise do;
put 'Unexpected ERROR: _iorc_= ' _iorc_;
stop;
end;
end;
end;
run;
proc print data=dairy;
run;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
RESULTS -- Example 1: Only first matching observation updated due to duplicates in master Obs Item Inventory Price 1 Milk 15 2.04 2 Milk 3 1.99 3 Soymilk 8 1.79 4 Eggs 24 1.29 5 Cheese 14 2.19 OUTPUT to SAS log Item not in stock --- Butter RESULTS -- Example 2 Forcing updates to occur on all duplicates in master Obs Item Inventory Price 1 Milk 15 2.04 2 Milk 3 2.04 3 Soymilk 8 1.79 4 Eggs 24 1.29 5 Cheese 14 2.19
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Statements ==> File-handling ==> MODIFY Common Programming Tasks ==> Combining Data SAS Reference ==> Statements ==> File-handling ==> MODIFY ==> with KEY= |
| Date Modified: | 2009-12-09 10:50:37 |
| Date Created: | 2004-09-30 14:08:59 |
| Product Family | Product | Host | SAS Release | |
| Starting | Ending | |||
| SAS System | Base SAS | All | n/a | n/a |




