Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Re: stata generated wrong lagged values for mi data

From (Yulia Marchenko, StataCorp LP)
Subject   st: Re: stata generated wrong lagged values for mi data
Date   Thu, 03 Jun 2010 17:51:58 -0500

Junqing <> asks how to generate a lagged variable for MI

> My data is panel data in mi format. I used the following syntax to generate
> several lagged variables(one time point lag) one by one:
> mi tsset ID time
> sort ID time
> by ID, gen lagx=l.x
> But i found that the generaged lagged values of panels were wrong. How i may
> fix it?

In general, when performing data manipulation on -mi- data, it is important to
use -mi- specific commands in place of the standard Stata commands when there
is an -mi- specific alternative.  The list of the -mi- specific commands can
be found in -help mi-.  If there is no -mi- specific version, before using the
standard Stata construct, look first at -help mi xeq-.  

To obtain correct results, Junqing needs to use the -mi xeq- command:

  . mi tsset ID time
  . mi xeq: sort ID time; by ID: gen lagx = l.x

Notice that to issue multiple commands with -mi xeq-, the commands must be
separated by a semicolon.  -mi xeq- issues all specified commands separately
on m=0,1,..., where m=0 refers to the original data and m=1,... refers to each
set of imputed data.  Because the lag operator requires that data are sorted
on the panel and time variables, it is important to use -mi xeq- to execute
both the -sort- command and -by: gen- together on each m=0,1,... data.

Martin Weiss demonstrates an example without using -mi xeq- which returns
results as expected:

> ***
> clear*
> set obs 100
> //id
> gen id=_n
> //expand to # of time periods
> expand 10
> bys id: gen time=_n
> gen x=rnormal()
> compress
> mi set wide
> mi tsset id time
> by id: gen lagx=l.x
> l in 1/50, sepby(id)
> ***

Depending on the chosen -mi- storage style, using standard Stata commands can
be appropriate as is the case in Martin's example, which used the -wide-
style.  However, it is safer to use -mi- specific commands or -mi xeq- because
these will work regardless of the -mi- style.

If Junqing is still experiencing difficulties, Junqing should contact Stata
Technical Services at

-- Yulia                           -- Wes    
*   For searches and help try:

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