I suggest using the retain statement.
DATA;
RETAIN N ; /* N is set to missing and will be retained in each subsequent iteration */
IF _N_=1 THEN N=0 ; /*first case N=0. All other cases N is still missing */
N=SUM(N, +1) ; /*FOR FIRST CASE N=0+1. The value of 1 is carried over to the second case and then 1 is added to it so the second case becomes 2. For the third case the value of 2 is retained, i.e. carries over, and 1 is added to this so N for the 3rd case is 3. And so on.*/
/* Using N=N+1 would work here as well as N=SUM(N, 1) but the latter form is good practice because it insures that a missing value for N will not make the result of the addition null or missing.*/
... View more