*---Usage of LAG function;

*---The lag function is a way to retrieve values of a variable from a previous record. 

    You may retrieve data from the last record or any of the previous 100 records by using

    the LAGN(variable) function (i.e., LAG1, LAG2, ..., LAG100).;

 

data test;

  input x;

  lag1=lag1(x);

  lag2=lag2(x);

  lag5=lag5(x);

  cards;

   1

   2

   3

   4

   5

   6

   7

   8

   9

  10

  ;

run;

 

proc print;

  title "Example Using LAG Function";

run;

 

 

/* SAS Ouput:

 

     Example Using LAG Function

 

 Obs     x    lag1    lag2    lag5

 

   1     1      .       .       .

   2     2      1       .       .

   3     3      2       1       .

   4     4      3       2       .

   5     5      4       3       .

   6     6      5       4       1

   7     7      6       5       2

   8     8      7       6       3

   9     9      8       7       4

  10    10      9       8       5

 

 

*/