Replaces an observation in the same location.
| Valid in: | DATA step |
|---|---|
| Category: | Action |
| Type: | Executable |
| Restrictions: | This statement is not supported in a DATA step that runs in CAS. |
| Use only with a MODIFY statement. |
Table of Contents
If you specify no argument, the REPLACE statement writes the current observation to the same physical location from which it was read in all data sets that are named in the DATA statement.
specifies the data set to which the observation is written.
| Requirement | The data set name must also appear in the DATA statement and in one or more MODIFY statements. |
|---|---|
| Tip | Instead of using a data set name, you can specify the physical pathname to the file, using syntax that your operating system understands. The pathname must be enclosed in single or double quotation marks. |
Using an explicit REPLACE statement overrides the default replacement of observations. If a DATA step contains a REPLACE statement, explicitly program all output for the step.
This example updates phone numbers in data set MASTER with values in data set TRANS. It also adds one new observation at the end of data set MASTER. The SYSRC autocall macro tests the value of _IORC_ for each attempted retrieval from MASTER. (SYSRC is part of the SAS autocall macro library.) The resulting SAS data set appears after the code:
data master;
input FirstName $ id $ PhoneNumber;
datalines;
Kevin ABCjkh 904
Sandi defsns 905
Terry ghitDP 951
Jason jklJWM 962
;
data trans;
input FirstName $ id $ PhoneNumber;
datalines;
. ABCjkh 2904
. defsns 2905
Madeline mnombt 2983
;
data master;
modify master trans;
by id;
/* obs found in master */
/* change info, replace */
if _iorc_ = %sysrc(_sok) then replace;
/* obs not in master */
else if _iorc_ = %sysrc(_dsenmr) then
do;
/* reset _error_ */
_error_=0;
/* reset _iorc_ */
_iorc_=0;
/* output obs to master */
output;
end;
run;
proc print data=master;
title 'MASTER with New Phone Numbers';
run;
