At the beginning of each iteration of the DATA step, SAS replaces variables values under program data vector with missing values. A RETAIN statement overrides this default values. RETAIN statement tells SAS not to set variables with missing values when going from the current iteration of the DATA step to the next i.e. SAS retains the values.
RETAIN Var1 Var2 …Varn;
Now look at below code and output, we want to generate a variable Y which should have cumulative values of X.
Above, we have not able to return the cumulative value. Now look at below code with RETAIN statement.
Above after each iteration value of Y was retained and it has returned the cumulative value of X.