>> Home >> Resources & support >> FAQs >> Stata 5: Creating lagged variables
Note: This FAQ is for users of Stata 5, an older version of Stata. It is not relevant for more recent versions.

Stata 5: How do I create a lag variable?

Title   Stata 5: Creating lagged variables
Author James Hardin, StataCorp

Create lag (or lead) variables using subscripts.

        . gen lag1 = x[_n-1]
        . gen lag2 = x[_n-2]
        . gen lead1 = x[_n+1]

You can create lag (or lead) variables for different subgroups using the by prefix. For example,

        . sort state year 
        . by state: gen lag1 = x[_n-1]

If there are gaps in your records and you only want to lag successive years, you can specify

        . sort state year
        . by state: gen lag1 = x[_n-1] if year==year[_n-1]+1

See [D] egen for details on creating variables of moving averages.

Stata

Shop

Support

Company


The Stata Blog: Not Elsewhere Classified Find us on Facebook Follow us on Twitter LinkedIn Google+ YouTube
© Copyright 1996–2016 StataCorp LP   •   Terms of use   •   Privacy   •   Contact us