Retain Statement

Contents Index


The retain statement is used to hold the values of variables accross iterations of the data step. Normally, all variables in the data step are set to missing at the start of each iteration of the data step.

The usage retain x y; retains the values of the variables x and y accross data step iterations. The usage retain; will retain the values of all variables used in the data step accross iterations of the data step.

As a simple example, look at the following program attempting to compute values of y(n)=2*y(n-1) with y(1)=1.

data dumb;
if _n_=1 then y=1;
else y=2*y1;
y1=y;
if _n_ =100 then stop;
run;

Since values are set to missing at the start of a data step iteration, the data set dumb will contain one value 1 and the other 99 values of y will be missing.

The following program produces the desired data set.

data smart;
retain y1;
if _n_=1 then y=1;
else y=2*y1;
y1=y;
if _n_ =100 then stop;
run;


Contents Index

Copyright © 1997 by Jerry Alan Veeh. All rights reserved.