LAG function

LAG. LAG(variable[, n]). Numeric or string. The value of variable in the previous case or n cases before. The optional second argument, n, must be a positive integer; the default is 1. For example, prev4=LAG(gnp,4) returns the value of gnp for the fourth case before the current one. The first four cases have system-missing values for prev4.

  • The result is of the same type (numeric or string) as the variable specified as the first argument.
  • The first n cases for string variables are set to blanks. For example, if PREV2=LAG (LNAME,2) is specified, blanks will be assigned to the first two cases for PREV2.
  • When LAG is used with commands that select cases (for example, SELECT IF and SAMPLE), LAG counts cases after case selection, even if specified before these commands. See the topic Command Order for more information.

Note: In a series of transformation commands without any intervening EXECUTE commands or other commands that read the data, lag functions are calculated after all other transformations, regardless of command order. For example,

COMPUTE lagvar=LAG(var1).
COMPUTE var1=var1*2.

and

COMPUTE lagvar=LAG(var1).
EXECUTE.
COMPUTE var1=var1*2.

yield very different results for the value of lagvar, since the former uses the transformed value of var1 while the latter uses the original value.