Announcement

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

  • Prefix for first observation in panel?

    I have three variables, id year effect. After using xtset id year, I want to calculate, for each id, the difference between the value of effect in each year and the value in the first year. Can I do this using lag prefixes? The help file for tsvarlist didn't provide an answer (as far as I can tell).

    Sean

  • #2

    Code:
    bysort id (year) : gen change = effect - effect[1]

    Comment


    • #3
      Thanks Nick. So I need the bysort...: even with xtset already invoked? This method also implies that xtset is not necessary.

      Comment


      • #4
        If you have just xtset, "just" meaning that you have not changed the sort order since you did that, then data are in good order, and

        Code:
        by id: gen effect = effect - effect[1]
        will be fine. The point is that the code does depend on effect[1] referring to the first time and for that an explicit sort does no harm.

        Note that the result will be missing if effect[1] is.

        Conversely, you indeed don't need xtset just for this.

        Comment

        Working...
        X