Stata: Data Analysis and Statistical Software
   >> 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
Date January 1996

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 [R] egen for details on creating variables of moving averages.

Bookmark and Share 
FAQs
What's new?
Statistics
Data management
Graphics
Programming Stata
Mata
Resources
Internet capabilities
Stata for Windows
Stata for Unix
Stata for Mac
Technical support
Like us on Facebook Follow us on Twitter Follow us on LinkedIn Google+ Watch us on YouTube
Follow us
© Copyright 1996–2013 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index   |   View mobile site