Announcement

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

  • Problem with setting a panel

    Dear all,

    I have a panel which looks like this:

    panel variable: Countryid (unbalanced)
    time variable: Year, 1995 to 2015
    delta: 1 unit

    Usually, I open a do file I've saved from previous sessions, which first generates several variables and then sets the data as a panel wih xtset Countryid Year.

    I just noticed that the fact that I'm using xtset only after my variables are generated makes stata modify these said variables (they take new values once I use xtset).

    Is it normal? Should I start using xtset before I generate these variables?

    Thank you in advance!
    Last edited by Tiago Oliveira; 08 Jul 2019, 17:44. Reason: panel

  • #2
    I doubt very much that what you say is true. Xtset shouldn’t change any variables. If you show us your code and output, or better yet provide us with a replicable example, we can assess better. My guess is that something else is causing the changes but you aren’t noticing them until you xtset.
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    Stata Version: 17.0 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

    Comment


    • #3
      Richard, thank you very much for your reply.

      I'll give you the example of what I'm trying to do right now. I'm interested in the regression for which the delta log of investment is equal to the delta log of GDP (and some other variables, but they don't matter right now).

      I have data for investment and GDP, so I just type:
      gen linv = log(totalinvestment)
      bysort Countryid: gen dlinv=(linv[_n]-linv[_n-1])/linv[_n-1]

      gen lgdp = log(gdp)
      bysort Countryid: gen dlgdp=(lgdp[_n]-lgdp[_n-1])/lgdp[_n-1]

      I can either use xtset before or after I generate these variables, but if I use it after I generate them it leads to different values for both dlinv and dlgdp. See, this is what the data for these two variables looks like before I type xtset:
      Click image for larger version

Name:	before.JPG
Views:	1
Size:	26.3 KB
ID:	1506717


      And this is what happens immediatly after I type xtset Countryid Year:

      Click image for larger version

Name:	After.JPG
Views:	1
Size:	32.9 KB
ID:	1506718


      I have no clue why this is happening...

      Comment


      • #4
        I think it is the same data, just sorted differently. The help for xtset says "If the data are set with a panelvar and a timevar, xtset also sorts the data by panelvar timevar if a timevar was specified. If the data are set with a panelvar only, the sort order is not changed."

        Also, see the Statalist FAQ, pt. 12, about posting Qs effectively. In particular, code tags are preferred over pasting images.

        I suspect you'd be better off xtsetting first. The data would already be sorted then.
        -------------------------------------------
        Richard Williams, Notre Dame Dept of Sociology
        Stata Version: 17.0 MP (2 processor)

        EMAIL: [email protected]
        WWW: https://www3.nd.edu/~rwilliam

        Comment


        • #5
          I'm also not sure if your gen commands are right. The bysort doesn't guarantee cases are sorted by year within country. which is what I'm guessing you want. If you xtset first you can use time-series notation, e.g. something like

          gen dlinv=(linv-L.linv)/L.linv
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          Stata Version: 17.0 MP (2 processor)

          EMAIL: [email protected]
          WWW: https://www3.nd.edu/~rwilliam

          Comment


          • #6
            xtsetting and using time series notation will also be good if, say, some years are missing for some countries. Then you won't be doing something like subtracting 2012's value from 2014's value because 2013 data is missing.
            -------------------------------------------
            Richard Williams, Notre Dame Dept of Sociology
            Stata Version: 17.0 MP (2 processor)

            EMAIL: [email protected]
            WWW: https://www3.nd.edu/~rwilliam

            Comment

            Working...
            X