Announcement

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

  • Yearly variable from Paneldata

    Hello there,

    I'm currently working with a paneldata set where observations are identified by a personal id (variable name: pid) and the year (variable name: syear). While this paneldata set includes a variable called KINDHIP, describing the kind of health insurance of individual i in year t, I need for replication reasons yearly variables like KINDHIP2007 describing the kind of health insurance person i had in year 2007. The value of this variable should be the same in all years for the single pids so that I can select all yearly observations for persons that had a certain value for KINDHIP in 2007. To make it more clear: I can't just use an if-clause since I don't just want e.g. all 2007-oberservations with KINDHIP == 2, but ALL observations over the whole time horizon for persons that have KINDHIP == 2 in 2007.

    I tried
    gen kindhip2007 = .
    by pid (syear), sort: replace kindhip2007 = KINDHIP[syear == 2007]

    and understood why this isn't working, but unfortunately can't find a solution.

    Edit: For some individuals there are certain years missing in the paneldata set and I need to treat these wholly missing years (there doesn't even exist a pid-syear-combination for these in the dataset) like missing-values.

    Thanks a lot!

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(pid syear KINDHIP)
    1 2005 1
    1 2006 1
    1 2007 2
    1 2008 2
    1 2009 3
    end
    
    gen kindhip2007 = KINDHIP if syear == 2007
    
    sort pid kindhip2007
    
    by pid: replace kindhip2007 = kindhip2007[1]
    
    drop kindhip2007
    
    levelsof syear, local(years)
    foreach year of local years {
    gen kindhip`year' = KINDHIP if syear == `year'
    bysort pid (kindhip`year'): replace kindhip`year' = kindhip`year'[1]
    }

    Comment


    • #3
      Thank you very very much, it's working perfectly fine!

      Comment

      Working...
      X