REPLACE Statement

REPLACE <range> <VAR operand> <WHERE(expression)> ;

The REPLACE statement replaces values of observations in a SAS data set.

The arguments to the REPLACE statement are as follows:

range

specifies a range of observations. You can specify a range of observations by using the ALL, CURRENT, NEXT, AFTER, and POINT keywords, as described in the section Process a Range of Observations.

operand

specifies a set of variables. As described in the section Select Variables with the VAR Clause, you can specify variable names by using a matrix literal, a character matrix, an expression, or the _ALL_, _CHAR_, or _NUM_ keywords.

expression

specifies a criterion by which certain observations are selected. If the WHERE clause is omitted, no subsetting occurs. The optional WHERE clause conditionally selects observations that are contained within the range specification. For details about the WHERE clause, see the section Process Data by Using the WHERE Clause.

The REPLACE statement replaces the values of observations in a SAS data set with current values of matrices with the same name. Use the range, VAR, and WHERE arguments to limit replacement to specific variables and observations. Replacement matrices should be the same type as the data set variables. The REPLACE statement uses matrix elements in row order, replacing the value in the $i$th observation with the $i$th matrix element. If there are more observations in range than matrix elements, the REPLACE statement continues to use the last matrix element.

For example, the following statements increment the weights of all males in a data set:

data class;
set Sashelp.Class;
run;

proc iml;
edit class;          /* open data set for edit */
read all var {weight} where(sex="M"); 
weight = weight + 5; /* add 5 to male weights */
replace all var {weight} where(sex="M");
close class;