Previous Page | Next Page

Language Reference

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:


specifies a range of observations.


selects a set of variables.


is evaluated for being true or false.

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 th observation with the 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 cause all occurrences of 'ILL' to be replaced by 'IL' for the variable STATE:

   replace all var{state} where(state="ILL");

You can use any of the following keywords to specify a range of observations:


all observations


the current observation

NEXT <number>

the next observation or the next number of observations


all observations after the current one

POINT operand

observations by number, where operand can be one of the following:



a single record number

point 5

a literal that contains several

point {2 5 10}

record numbers


the name of a matrix

point p

that contains record numbers


an expression in parentheses

point (p+1)

If the current data set has an index in use (see the INDEX statement, the POINT option is invalid.

You can specify a set of variables to use with the VAR clause. The variables argument can have the following values:

  • a literal that contains variable names

  • the name of a matrix that contains variable names

  • an expression in parentheses that yields variable names

  • one of the keywords described in the following list:


    for all variables


    for all character variables


    for all numeric variables

The following examples demonstrate each possible way you can use the VAR clause.

   var {x1 x5 x9};          /* a literal matrix of names         */
   var x;                   /* a matrix that contains the names  */
   var("x1":"x9");          /* an expression                     */
   var _all_;               /* a keyword                         */

The WHERE clause conditionally selects observations, within the range specification, according to conditions given in the clause. The general form of the WHERE clause is

WHERE (variable comparison-op operand) ;

The arguments to the WHERE clause are as follows:


is a variable in the SAS data set.


is any one of the following comparison operators:


less than


less than or equal to


equal to


greater than


greater than or equal to


not equal to


contains a given string


does not contain a given string


begins with a given string


sounds like or is spelled like a given string


is a literal value, a matrix name, or an expression in parentheses.

WHERE comparison arguments can be matrices. For the following operators, the WHERE clause succeeds if all the elements in the matrix satisfy the condition:


=   ?   <   <=   >   >=

For the following operators, the WHERE clause succeeds if any of the elements in the matrix satisfy the condition:


=   ?   =:   =*

Logical expressions can be specified within the WHERE clause by using the AND (&) and OR (|) operators. The general form is


clause & clause

(for an AND clause)


clause | clause

(for an OR clause)

where clause can be a comparison, a parenthesized clause, or a logical expression clause that is evaluated by using operator precedence.

Note:The expression on the left-hand side refers to values of the data set variables, and the expression on the right-hand side refers to matrix values.

The following statement replaces all variables in the current observation:

Previous Page | Next Page | Top of Page