Using RETAIN in SAS to remember values

Using RETAIN in SAS to remember values

  • Post author:
  • Post category:Base SAS
  • Post comments:0 Comments
  • Reading time:4 mins read

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
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="https://9to5sas.com/set-statement-sas/">SET</a>, MERGE or <strong>UPDATE</strong> statement.
  • Variables whose value is assigned in a <a class="rank-math-link" href="https://9to5sas.com/sas-numeric-functions/#SUM">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;
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;

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

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.