MET MA 603: SAS Programming and Applications
MET MA 603:
SAS Programming and Applications
Retain and Sum Statements
1
1
The Retain Statement
By default, SAS processes data one observation at a time. After executing each statements in the Data step, SAS moves on to the next observation, setting initial values back to missing. Any information from the previous observation is lost.
The Retain Statement instructs SAS to remember the value of a variable from the previous observation. This allows the information to be used in processing the subsequent observations.
retain running_total 0 ;
In the retain statement, indicate the variable to be retained, as well as its initial value. More than one variable can be retained in the same statement.
2
2
The Sum Statement
The Sum Statement is a short-hand, special case of the running total version of the Retain statement. It allows for much of the code ordinarily needed to execute the Retain statement to be omitted.
running_total + number;
Note that the Sum statement only works to calculate a running total; for anything other than a running total, the full Retain statement must be used.
Also note, the Sum statement is different than the Sum function, and that the Sum statement does not actually use the word sum in its syntax.
3
3
Practice
Use the Loss.sas7bdat dataset for the following:
Use the Sum statement to calculate the total of the Amount.
Use the Retain statement to calculate the total of the Amount for observations where the Date is after 2010.
Use the Retain statement to calculate the total of the Amount separately (in the same Data step) for when WeatherFlag=0 and for when WeatherFlag=1.
4
4
Readings
Textbook section 3.10
5
5
/docProps/thumbnail.jpeg