Using RETAIN in SAS to remember values

RETAIN in SAS is used to “remember” values from previous observations. Variables that do not come from SAS data sets are, by default, set to a missing value during each iteration of the DATA step.

A RETAIN statement allows you to tell SAS not to set missing values to the variables during each iteration of the data step.

Basic Usage of Retain in SAS

data Example;
 input profit;

data example1;
set example;

data example1;
set example;
retain cum_sum 0;
Retain in SAS
Without Retain Statement
Retain in SAS
With Retain Statement

In the above example, SAS resets the values of cum_sum to missing for each observation.

By adding the RETAIN statement, the values of cum_sum are retained for the next iteration.

Points to Remember

  • If you do not specify any variable names, then SAS retains the values of all variables created in an INPUT or assignment statement.
  • SAS sets the initial value of a variable to be retained to missing if you don’t specify an initial value.
  • It is also important to understand what retain does and what it does not.

The following items need not require in a RETAIN statement since their values are implicitly retained in a data step.

  • Variables that are read with a <a class="rank-math-link" href="">SET</a>, MERGE or <strong>UPDATE</strong> statement.
  • Variables whose value is assigned in a <a class="rank-math-link" href="">SUM</a> statement
  • Variables that are created by the IN = option

The RETAIN statement is not executable. Therefore it can appear anywhere in the DATA step.

Retain in SAS with BY Groups​

For each age group, you want to check if BMI for that age is less than 18.5.

proc sort data=sashelp.bmimen out=bmi;
 by age;
data underweight;
length underweight $3.;
set bmi;
by age;
retain underweight;
if first.age then underweight="NO";
if bmi lt 18.5 then underweight="YES";
if last.age then output;

proc print data=underweight(firstobs=215 obs=225);

Every week we'll send you SAS tips and in-depth tutorials


Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.