Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting "not sorted" error

    Hi there,

    I am running the xtreg command on Stata 17 MP on a Mac M2 to look at the impact of protests on economic values (eg stocks). My panel data is at the city level. To set up the panel, I run

    Code:
    sort city_num date_num
    xtset city_num date_num, daily
    Then running the regression
    Code:
    xtreg spread_us L1.event_dummy $all_controls i.year_month
    yields the error 'not sorted' due to this use of the lagged event_dummy variable. However, if I instead initialize the lagged variable beforehand and then run the regression, like so:

    Code:
    gen event_dummy_1d_lag = L1.event_dummy
    xtreg spread_us event_dummy_1d_lag $all_controls i.year_month
    everything works fine. Any advice on what Im doing wrong would be appreciated.

    Best,
    Sam
    Last edited by Sam Lindquist; 18 Aug 2023, 15:24.

  • #2
    Hi Sam, welcome to the forum.

    This shouldn't happen. Are you sure you don't run any code between -xtset- and -xtreg- that we aren't seeing here? Especially anything that might implicitly sort your data. sorting by city_num and date_num is redundant, because -xtset- will sort the data for you. It is immaterial that initializing the lagged variable beforehand yourself doesn't result in an error, because Stata assumes the new variable isn't lagged. It is probably also invalid to use the event_dummy_1d_lag variable in general, since (e.g.) missing values wont be handled correctly.

    Do you still get the error with these two lines together?

    Code:
    sort city_num date_num
    xtreg spread_us L1.event_dummy $all_controls i.year_month
    What happens when you clear everything, reload the data from the file system, then run these two lines together?

    Code:
    xtset city_num date_num, daily
    xtreg spread_us L1.event_dummy $all_controls i.year_month

    Comment


    • #3
      Hi Daniel,

      I tried this advice and it worked great. Thank you! I was in fact sorting in between the -xtset- command and the regressions. I wasn't aware this changed the structure of the panel data. I reran the -xtset- command right before the regressions and it worked.

      Best,
      Sam

      Comment

      Working...
      X