By default, SAS sets variables created in a DATA step to missing at the start of each new iteration unless they are read from a dataset or otherwise automatically retained
The `retain` statement tells SAS to preserve the value of a variable from one iteration to the next
This is especially useful for counters, running totals, and group-level calculations
Create input dataset
The dataset `scores` contains test-level scores for each subject
We will sort this dataset by subject and then use retained variables inside BY group processing examples
SAS Log
Each subject has one or more rows in the input dataset
This structure is ideal for demonstrating retained counters and running statistics by subject
Dataset View
Find the total number of records for each student
We sort by `usubjid` so BY group processing works correctly
The retained variable `ntests` keeps its value across rows of the same subject
When a new subject begins, `first.usubjid` resets the counter
SAS Log
`ntests` shows a running count of rows within each subject
Inspect the last row of each subject to identify the total number of records for that subject
Dataset View
Find the running total score for each subject
The retained variable `total` carries forward the accumulated score value within each subject
At the start of a new subject, the total is reset to the first score of that subject
SAS Log
The variable `total` is a running cumulative score within each subject
Check the last row for each subject to see the final total score
Dataset View
Find the highest score for each subject
The retained variable `max_score` stores the highest value seen so far within the subject
It is initialised on the first row of each subject, and updated only when a higher score is found
SAS Log
`max_score` tracks the running maximum within each subject
Look at the final row of each subject to confirm the highest score across that subject's tests
Dataset View
Find the lowest score for each subject
The retained variable `min_score` works the same way, but keeps the smallest score seen so far
This pattern is common for deriving minimum and maximum visit values within groups
SAS Log
`min_score` tracks the running minimum within each subject
Verify the final value on the last row of each subject against the subject's individual scores
Dataset View
Key points to remember
`retain` preserves a variable's value across DATA step iterations
`retain` is often used together with `first.` and `last.` BY group indicators
Retained variables are useful for counters, running totals, running maximums, and running minimums