Announcement

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

  • Replace value

    Hi!

    How can I replace the value of one variable X in period T for the value of the variable X in the previous period T-1?

    replace X = ??? if year ???

    Thanks!



  • #2
    Why not just add 1 to the year?

    Code:
    replace year = year + 1

    Comment


    • #3
      What Joseph suggests would do the same operation for all variables, not just a single one.
      You can do, assuming it is panel data:
      Code:
      bysort id year: replace x = x[_n-1]
      And note also that Stata allows time operators in regressions, which means ha in most cases you do not have to manually create lagged variables.
      Example:
      Code:
      xtreg outcome varY varZ L1.varX

      Comment


      • #4
        Jorrit Gosens is pointing in a good direction but the code would be better as

        Code:
        bysort id (year) :
        and the replacement should be conditional if missing(x). it needs to be more complicated still if there are gaps in the data.
        Last edited by Nick Cox; 01 Oct 2019, 01:26.

        Comment


        • #5
          The best approach is probably

          Code:
          tsset id year 
          replace x = L.x if missing(x)
          There is a wider discussion at https://www.stata.com/support/faqs/d...issing-values/ . But even if interpolation is a good idea, there may well be better ways to do it.

          Comment

          Working...
          X