HomeContact
Base SAS
Using RETAIN in SAS to remember values
Subhro Kar
Subhro Kar
January 11, 2020
1 min

Table Of Contents

01
Basic Usage of Retain in SAS
02
Retain in SAS with BY Groups​
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;
datalines;
12
54
14
44
45
;
run;
data example1;
set example;
cum_sum=cum_sum+profit;
run;
data example1;
set example;
retain cum_sum 0;
cum_sum=cum_sum+profit;
run;

![Retain in SAS](../../assets/retain-in-sas/retain.png)

Without Retain Statement

![Retain in SAS](../../assets/retain-in-sas/retain-sas.png)

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 [SET](https://9to5sas.com/set-statement-sas/), MERGE or **UPDATE** statement.
  • Variables whose value is assigned in a [SUM](https://9to5sas.com/sas-numeric-functions/#SUM) 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;
run;
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;
run;
proc print data=underweight(firstobs=215 obs=225);
run;

Share


Related Posts

Data Cleaning in SAS: A Complete Guide with Examples
December 19, 2024
2 min
© 2025 9to5sas
AboutContactPrivacyTerms