Announcement

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

  • Using for each for observations in stata

    For each observations, variable x takes the value 1 in year 2000, how to make the value of x =1 in subsequent years ?

    Any help is greatly appreciated

  • #2
    You're not telling us much about your data. I am going to guess at some kind of panel or longitudinal layout with an identifier as well as a year variable.

    Code:
    gen wanted = x == 1 & year == 2000 
    bysort id (year) : replace x = 1 if x[_n-1] == 1
    Or

    Code:
    bysort id (year) : gen wanted = sum(x == 1 & year == 2000)

    Comment


    • #3
      without a -dataex- example (see the FAQ), it is not possible to give exact code but my best guess is that, after sorting, you just want something like:
      Code:
      replace x=1 if year>2000
      but note that this makes a number of assumptions that may not be true

      note that it is very unlikely that loop is needed here

      Comment


      • #4
        Thanks Nick and Rich> Mine is an unbalanced panel data with firm_id and year.

        r new_product_dummy

        . rename new_product_dummy x

        . dataex Factory_Code year x

        ----------------------- copy starting from the next line -----------------------
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str8 Factory_Code float(year x)
        "0000003F" 2000 0
        "0000003F" 2002 1
        "0000006F" 2010 0
        "0000009F" 2001 0
        "0000028F" 2011 0
        "0000033F" 2005 1
        "0000033F" 1999 0
        "0000033F" 2001 0
        "0000033F" 2000 0
        "0000033F" 2002 0
        "0000033F" 2010 1
        "0000103F" 2004 1
        "0000103F" 2011 1
        "0000103F" 2003 1
        "0000103F" 2000 0
        "0000106F" 2015 0
        "0000106F" 2014 1
        "0000108B" 2006 0
        "0000108B" 2015 0
        "0000108B" 2013 1
        "0000110F" 2013 0
        "0000129F" 2000 0
        "0000129F" 2005 1
        "0000133F" 2003 0
        "0000133F" 2015 1
        "0000133F" 2009 1
        "0000133F" 2010 0
        "0000206F" 2009 1
        "0000206F" 2011 1
        "0000206F" 2010 1
        "0000206F" 2015 0
        "0000206F" 2016 1
        "0000206F" 2013 1
        "0000206F" 2008 1
        "0000206F" 2014 0
        "0000208B" 2009 1
        "0000208B" 2006 0
        "0000208B" 2012 0
        "0000208B" 2011 1
        "0000208B" 2010 0
        "0000208B" 2007 0
        "0000208B" 2008 0
        "0000208B" 2013 0
        "0000209F" 2005 0
        "0000209F" 2013 1
        "0000209F" 2003 0
        "0000219F" 2004 0
        "0000220F" 2014 0
        "0000220F" 2015 0
        "0000233F" 2005 0
        "0000233F" 2009 1
        "0000233F" 2011 1
        "0000306F" 2008 0
        "0000306F" 2007 0
        "0000306F" 2014 1
        "0000308B" 2011 1
        "0000308B" 2007 0
        "0000308B" 2012 0
        "0000319F" 2005 1
        "0000319F" 2004 0
        "0000333F" 2006 0
        "0000333F" 2011 1
        "0000333F" 2013 1
        "0000406F" 2008 1
        "0000406F" 2007 0
        "0000408B" 2006 0
        "0000408B" 2010 1
        "0000408B" 2016 1
        "0000419F" 2004 0
        "0000419F" 2005 1
        "0000420F" 2011 0
        "0000424F" 2003 1
        "0000424F" 1999 0
        "0000424F" 2007 0
        "0000424F" 2001 1
        "0000429F" 2001 0
        "0000429F" 2013 0
        "0000429F" 2011 1
        "0000503F" 2004 0
        "0000503F" 2000 0
        "0000503F" 2015 1
        "0000519F" 2004 0
        "0000519F" 2013 1
        "0000520F" 2011 0
        "0000523F" 2015 0
        "0000527F" 2006 0
        "0000527F" 2009 0
        "0000527F" 2011 1
        "0000527F" 2003 0
        "0000533F" 2008 1
        "0000533F" 2001 0
        "0000533F" 2000 0
        "0000603F" 2006 0
        "0000609F" 2007 1
        "0000609F" 2004 0
        "0000610F" 2011 0
        "0000619F" 2003 1
        "0000619F" 2002 0
        "0000619F" 2004 1
        "0000620F" 2015 0
        end

        Comment


        • #5
          Some variation on #2 remains my guess. Whether it's firm identifier or factory code you care about only you can say.

          Comment

          Working...
          X