Some simple examples of using these statements are:
The RETAIN statement simply tells SAS not to change the value of a listed variable until it is replaced in the code. It is usually placed near the top of a DATA step. If you want the initial value of the variable to be something other than missing, you can list it as well.
Example:
DATA newdata; SET olddata; RETAIN Total 0; Total = Total + Score; RUN;Total starts with the value of 0 and each observation adds to the Total its value of Score. The very last observation will contain the sum of the Scores for all the observations.
One problem can occur when adding together two variables is SAS. If either of the variables is missing, then the sum of the two will be missing. If there is a chance that either variable may be missing, it is better to use the SUM function. The SUM function ignores missing values.
DATA newdata; SET olddata; RETAIN Total 0; Total = SUM(Total, Score); RUN;
DATA newdata; SET olddata; IF _N_ = 1 then Total = 0; Total + Score; RUN;We don't need to tell SAS to retain the value of Total across observations. It is a shortcut method of the RETAIN statement. However, we do need to set the initial value of Total. _N_ is a SAS Automatic variable that exists only within the DATA step. _N_ contains the observation number SAS is currently processing. So IF _N_ = 1 asks SAS if it is at the first observation. If it is, then it initializes the value of Total to 0.