Friday, January 15, 2016

Calculating the difference between the last value and the first value using RETAIN statement

The program below demonstrates the use of retain statement to calculate the difference between the last value and the first value for each id. 

data x1;
       input id days score;
 datalines;
1 1 23
1 2 34
2 1 45
2 2 46
3 1 35
3 2 40
;

proc sort; 
    by id;
run;

data new;
     set x1;
     by id;   
      if first.id then do;
           score1=score;
           retain score1;
        end;
   diff=score-score1;
   if last.id then output;
run;

proc print;
run;



No comments:

Post a Comment