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;
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>,
- 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
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;