Announcement

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

  • Carrying forward values under conditions of another variable

    Dear Statalist,

    Suppose I have the following dataset:

    Code:
    input state dist year value
    1 1 1990 10
    1 1 1991 20
    1 1 1992 30
    1 1 1993 40
    1 1 1994 50
    1 1 1995 60
    1 1 1996 70
    1 1 1997 80
    1 1 1998 90
    1 1 1999 100
    1 1 2000 110
    end
    My object is to use certain values for different sets of years (of unequal length). For example, I would like to use the 1990 value for all years between 1990-1993, the 1994 value for all years between 1994-1995, and the 1996 value for all years between 1997-2000.

    While at first I thought I could use a replace with an [_n-#] modifier, the unequal time spans (and sometimes missing start/end points) are proving challenging. Is there an alternative to replace I am missing?

    Best,

    Jeff



  • #2
    I assume you want to do this -by- state and dist.

    Code:
    gen new_value = value if inlist(year, 1990, 1994, 1996)
    by state dist (year), sort: replace new_value = new_value[_n-1] if missing(new_value)

    Comment


    • #3
      Clyde to the rescue again! To think of all those you have helped.

      Not sure why my attempt with -bysort- was going awry. Thank you very kindly!

      Comment


      • #4
        https://www.stata.com/support/faqs/d...issing-values/ gives general discussion.

        Comment

        Working...
        X