Amadeus cookies policy - you'll see this message only once.

Amadeus use cookies on this website. They help us to know a little bit about you and how you use our website, which improves the browsing experience and marketing - both for you and for others. They are stored locally on your computer or mobile device. To accept cookies, continue browsing as normal. Or, go to the privacy policy for more information.

Getting Previous Values of the Same Variable

The LAG and DIF functions can be used within a data step to get the value of a variable from a previous record. The LAG function returns the value of a variable from 'x' records above the current record. When you use the LAG function, a queue is set up so that values of the variable starting at record 'x' are stored. The DIF function works in the same way, except that it returns the difference between the current value and the value 'x' records above it.

The following code demonstrates the use of the two functions:

data t;
  input x;
  first_lag=lag(x);
  second_lag=lag2(x);
  first_dif=dif(x);
  second_dif=dif2(x);
cards;
1
2
4
9
run;

Below is the resulting data set:

     first_    second_    first_    second_
x      lag       lag        dif       dif
1 . . . . 2 1 . 1 . 4 2 1 2 3 9 4 2 5 7

Warning:
Be very careful when using the LAG and DIF functions within conditional sections of a data step, as lag(x) is NOT "the value of X in the last observation"; rather, it is "the value of X last time we executed the statement which calls the lag function".