Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: Re: Same code, same machine, same data, different results


From   Christopher Baum <kit.baum@bc.edu>
To   "<statalist@hsphsun2.harvard.edu>" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Re: Same code, same machine, same data, different results
Date   Fri, 7 Sep 2012 11:02:12 +0000

<>
On Sep 7, 2012, at 2:33 AM, Mattia wrote:

> There is no m:m merge, and the same variables are dropped each time. The
> problem was indeed one of sorting, as two of the answers mentioned,
> together with a wrong use of indexing. There was a command such as
> (simplifying)
> 
> sort id year
> generate a_lag = a[_n - 1] if id == id[_n - 1]
> 
> the dataset is quarterly, so it has more than one observation per year.
> Each time I ran the code, the dataset had a slightly different sorting and
> some rows were assigned each time different values.

This points out the problem with using the [_n - 1] syntax as opposed to making use of -tsset- or -xtset-.
If you use one of those commands, you do not have to worry about sorting the data, and you could use
something like

g yearqtr = yq(year, qtr)
tsset id yearqtr
g a_lag - L.a

if you really wanted to have such a variable handy. Otherwise, you wouldn't even need to create
that lagged variable, and could merely say something like regress a L.a.

Avoiding [_n -1] is particularly important in panels, as otherwise you always have to keep checking to
ensure that you're not looking back (or ahead) into a different panel. Using -tsset- or -xtset- avoids all
that, as Stata will enforce panel boundaries without fail.

Kit

Kit Baum   |   Boston College Economics & DIW Berlin   |   http://ideas.repec.org/e/pba1.html
                             An Introduction to Stata Programming  |   http://www.stata-press.com/books/isp.html
  An Introduction to Modern Econometrics Using Stata  |   http://www.stata-press.com/books/imeus.html


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index