First. and Last. Automatic Variables
In the program above there are two new variables - with two level names! Whenever you use a BY statement in a data step, two automatic variables are created for each variable in the data set. FIRST.varname indicates if the current observation is the first one with the current value of the variable (=1 if yes, =0 if no).
LAST.varname indicates if the current observation is the last one with the current value of the variable (=1 if yes, =0 if no).
Example:
Data NumVisit;
Set PatVisit;
By PatNum;
Retain NumVisit;
If first.PatNum then NumVisit = 0;
Numvisit = NumVisit + 1;
If last.PatNum then Output;