Announcement

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

  • Panel Data - Identify how many panelists have a change in a particular variable over time

    Hello all,

    I'm working with a fixed effects panel data model (individual FE). I have a number of individual-level independent variables. It is obvious that many of these individual-level variables change over time or each panelist (e.g. age). However, I have one individual-level variable that may or may not change over time, depending on the individual (STATA does not reject due to collinearity). I would like to determine exactly how many panelists experienced any change in this independent variable. What code could I use?

  • #2
    Max, a simple example as below. "totalnum" records the number of panelists experiencing any temporal change in "var". If "var" is a string variable, you would need another code.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id time var)
    1 2000 3
    1 2001 3
    1 2002 .
    2 2000 1
    2 2001 1
    2 2002 2
    3 2000 5
    3 2001 5
    3 2002 5
    end
    
    bys id: egen v = sd(var)
    bys id: replace v = . if _n != 1
    egen totalnum = total(v>0 & v<.)
    drop v

    Comment


    • #3
      This is an FAQ: see https://www.stata.com/support/faqs/d...ions-in-group/ from which it follows that

      Code:
      bysort id (var) : gen different = var[1] != var[_N]
      creates a flag for change within a panel. Strictly, this may catch some values being missing and the other values being constant. So, more cautious code would segregate missings first:

      Code:
      gen OK = !missing(var) 
      
      bysort OK id (var) : gen different = (var[1] != var[_N])  & OK 
      bysort id (different) : replace different = different[_N]
      so that recipe gives

      1 if there was change

      0 if the non-missing values didn't change OR all values are missing

      and different needs call for yet different code.

      Comment


      • #4
        Thanks! I appreciate the assistance!

        Comment

        Working...
        X