Announcement

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

  • How to fill the previous missing value with the non missing one when there are multiple nonmissing value within one group?

    Hi all,
    I want to ask for help regarding filling missing value. I have a panel data, the id is permco(firm identifier), time var is year (calendar time).
    For each permco, I have year from 1974-2020 as below. Each firm receive some treatments at different years. For instance, 3047 receive treatments at year 1989, 1998, 2005, 2010, 2018. My goal is to replace ma_year of 3047 before 1989 with 1989, before 1998 with 1998, before 2005 with 2005. And the data contain similar cases for other firm (e.g. as you can see from the example, also firm 4557). Does anyone know how to do this?
    My final goal is to generate pretrend variable, for instance 3047 1985 will be -4, 3047 1986 will be -3... 3047 2007 will be -3, 3047 2008 will be -2, 3047 2009 will be -1

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long permco float(year ma_year)
    3047 1974    .
    3047 1975    .
    3047 1976    .
    3047 1977    .
    3047 1978    .
    3047 1979    .
    3047 1980    .
    3047 1981    .
    3047 1982    .
    3047 1983    .
    3047 1984    .
    3047 1985    .
    3047 1986    .
    3047 1987    .
    3047 1988    .
    3047 1989 1989
    3047 1990    .
    3047 1991    .
    3047 1992    .
    3047 1993    .
    3047 1994    .
    3047 1995    .
    3047 1996    .
    3047 1997    .
    3047 1998 1998
    3047 1999    .
    3047 2000    .
    3047 2001    .
    3047 2002    .
    3047 2003    .
    3047 2004    .
    3047 2005 2005
    3047 2006    .
    3047 2007    .
    3047 2008    .
    3047 2009    .
    3047 2010 2010
    3047 2011    .
    3047 2012    .
    3047 2013    .
    3047 2014    .
    3047 2015    .
    3047 2016    .
    3047 2017    .
    3047 2018 2018
    3047 2019    .
    3047 2020    .
    4557 1974    .
    4557 1975    .
    4557 1976    .
    4557 1977    .
    4557 1978    .
    4557 1979    .
    4557 1980    .
    4557 1981    .
    4557 1982    .
    4557 1983    .
    4557 1984    .
    4557 1985    .
    4557 1986    .
    4557 1987    .
    4557 1988    .
    4557 1989    .
    4557 1990    .
    4557 1991    .
    4557 1992    .
    4557 1993    .
    4557 1994    .
    4557 1995 1995
    4557 1996    .
    4557 1997    .
    4557 1998    .
    4557 1999    .
    4557 2000    .
    4557 2001    .
    4557 2002    .
    4557 2003    .
    4557 2004    .
    4557 2005    .
    4557 2006 2006
    4557 2007    .
    4557 2008    .
    4557 2009    .
    4557 2010 2010
    4557 2011    .
    4557 2012    .
    4557 2013 2013
    4557 2014    .
    4557 2015    .
    4557 2016 2016
    4557 2017    .
    4557 2018    .
    4557 2019    .
    4557 2020    .
    4620 1974    .
    4620 1975    .
    4620 1976    .
    4620 1977    .
    4620 1978    .
    4620 1979    .
    end
    Thank you a lot!

  • #2
    FAQ https://www.stata.com/support/faqs/d...issing-values/

    Comment


    • #3
      Hi Nick, thank you for the reply.
      I notice this and try replace var=var[_n+1] if !nonmissing(var), but this can only replace the missing observation right above the nonmissing one, e.g.3047 1988, but cannot replace observation before 3047 1988. I wonder if there is a command to replace everything? Or I'm thinking if it is possible to write a loop to repeat this command until 0 real change is made, but is this possible in stata?

      Comment


      • #4
        You need to read the entire FAQ which includes the device of reversing time temporarily for copying values back in time.

        Comment

        Working...
        X