Announcement

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

  • Carrying Values Backward by Group

    Hi all,

    I have a panel of individual-level survey data with three variables of interest. id signifies a local geography tag, so id can repeat, as can year. Var is a signifier of a policy adopted by a municipality. It looks something like this:
    id year var
    1 2013 0
    1 2013 0
    1 2014 0
    1 2015 1
    2 2013 0
    2 2014 0
    2 2015 0
    2 2015 0
    I want to fill backward such that each locality that has adopted the policy has var = 1 in each year, regardless of the year of adoption. I want to tell the program to essentially carry backward any observation of 1 starting in 2015 back to 2013, sorted by id. Notably, I want nothing to change for id = 2, since that region has not adopted the policy.

    I tried
    Code:
    tsset id
    year, but the console tells me "repeated time values within panel." This makes sense, because there is no way to identify who is a unique respondent across years and each id / year can repeat multiple times. I also tried
    Code:
    bysort id (year): replace var = 1 if var[_n+1] == 1
    but no changes were made. Any sense on how to do this?
    Last edited by Mark Weiss; 27 Oct 2022, 08:21.

  • #2

    Code:
    egen wanted = max(var) , by(id)
    Otherwise this is an FAQ. https://www.stata.com/support/faqs/d...issing-values/

    However

    Code:
    tsset id year
    was what you need for other purposes, although it doesn't help here.

    Comment


    • #3
      The first line of code worked, and what an elegant solution! Didn't even occur to me. Thanks for the help, as always.

      Comment

      Working...
      X