Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: creating lags

From   Chris Rohlfs <>
Subject   Re: st: creating lags
Date   Sat, 29 Mar 2003 22:00:30 -0600 (CST)

to create lags with a panel dataset, do the following:

if "id" is your cross-section identifier & "year" is your time variable,

sort id year
tsset id year

* now you can create lags using the lag operator lN. for the Nth lag.
* to create the 5th lag of the variable x, then we do:

gen l5x = l5.x

* (that's the letter l and then the number 5)  alternatively, fN. gives the Nth lead.

-- note, tsset only works if your cross-section identifier is a numeric
variable.  if your cross-section variable is a string, then you'll have to
do something to turn it into a numeric variable first.  one way to do this

egen n_id = group(id)

take care,

On Sat, 29 Mar 2003, Maria Popova wrote:

> Hi all,
> I have a time series/cross section data set and I'm trying to create 5-year
> lags for several variables.  I have data on 158 countries between 1966 and
> 1997, but of course since not all countries existed for the entire period,
> different countries have data for different sets of years.  I've sorted the
> data by time and within the time groups by country and then I tried
> creating the lags with:
> gen lag5_var=var[_n-790] (because 158*5=790)
> However, the resulting lag is incorrect... What am I doing wrong?  Do I
> have to generate missing values for the country-years that are not in the
> data set?  I thought Stata is supposed to do that for me...
> Thanks!
> *
> *   For searches and help try:
> *
> *
> *

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index