Previous Page | Next Page

Using More Than One Observation in a Calculation

Review of SAS Tools


Statements

RETAIN variable-1 < . . . variable-n>;

retains the value of the variable for use in a subsequent observation. The RETAIN statement prevents the value of the variable from being reinitialized to missing when control returns to the top of the DATA step.

The RETAIN statement affects variables that are created in the current DATA step (for example, variables that are created with an INPUT or assignment statement). Variables that are read with a SET, MERGE, or UPDATE statement are retained automatically; naming them in a RETAIN statement has no effect.

The RETAIN statement can assign an initial value to a variable. If you need a variable to have the same value in all observations of a DATA step, it is more efficient to put the value in a RETAIN statement rather than in an assignment statement. SAS assigns the value in the RETAIN statement when it is compiling the DATA step, but it carries out the assignment statement during each execution of the DATA step.

The plus sign is required in the sum statement; to subtract successive values from a starting value, add negative values to the sum variable.

SET SAS-data-set <END=variable>;

reads from the SAS-data-set specified. The variable specified in the END= option has the value 0 until SAS is processing the last observation in the data set. Then the variable has the value 1. SAS does not include the END= variable in the data set that is being created.

variable + expression;

is called a sum statement; it adds the result of the expression on the right side of the plus sign to the variable on the left side of the plus sign and holds the new value of variable for use in subsequent observations. The expression can be a numeric variable or expression. The value of variable is retained. If the expression is a missing value, the variable maintains its previous value. Before the sum statement is executed for the first time, the default value of the variable is 0.

The plus sign is required in the sum statement; to subtract successive values from a starting value, add negative values to the sum variable.

Previous Page | Next Page | Top of Page