Announcement

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

  • How to replace the previous value of categorical variable based on the current value

    Hi, as you can see the data, the variable group has many missing values for all pre-2004 data.
    I want to assign them the same value based on post-2004 values.
    For example, if firmid 000020's group is 30 at 2004, all the missing values before 2004 should be 30
    and if has a missing value at 2004, all the previous data must have missing values and so on.

    I can see the code is similar to the following but I can't really figure out the precise one.
    Code:
      ​​​​​​
    bys firmid (year): replace group = group[_n+1] if missing(group)

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str6 firmid float year double asset float manuf byte group
    "000020" 2002   47104379 1  .
    "000020" 1998   62195180 1  .
    "000020" 2004   48186088 1 30
    "000020" 1996   28259495 1  .
    "000020" 1992   15407567 1  .
    "000020" 1991   11917110 1  .
    "000020" 1995   28833199 1  .
    "000020" 1993   15893377 1  .
    "000020" 2001   48119915 1  .
    "000020" 1994   19038416 1  .
    "000020" 2005   49561086 1 30
    "000020" 2006   51361290 1 30
    "000020" 2007   53292551 1 30
    "000020" 2009  168856287 1 30
    "000020" 2003   46995322 1  .
    "000020" 2008   71262653 1 30
    "000020" 1999   59179568 1  .
    "000020" 2000   50412797 1  .
    "000020" 1997   27451435 1  .
    "000020" 1990    8948042 1  .
    "000070" 2004  374350110 0 10
    "000070" 2008  370114418 0 30
    "000070" 2000  603212077 0  .
    "000070" 2005  374938826 0 10
    "000070" 2003  376814521 0  .
    "000070" 2009  357268795 0 30
    "000070" 1991  180166552 0  .
    "000070" 2001  384778757 0  .
    "000070" 1996  421634354 0  .
    "000070" 2002  367042118 0  .
    "000070" 1994  370443516 0  .
    "000070" 1990  207929312 0  .
    "000070" 2007  375073262 0 10
    "000070" 1993  263818065 0  .
    "000070" 1998  672723790 0  .
    "000070" 1995  401364769 0  .
    "000070" 2006  381324320 0 10
    "000070" 1999  626625801 0  .
    "000070" 1992  219009219 0  .
    "000070" 1997  429658126 0  .
    "000100" 1998  152348178 1  .
    "000100" 2001  163151716 1  .
    "000100" 1990   14994625 1  .
    "000100" 1995  129153715 1  .
    "000100" 1992   17593937 1  .
    "000100" 1996  144924321 1  .
    "000100" 1997  153489898 1  .
    "000100" 2007  297178614 1 30
    "000100" 1991   16022598 1  .
    "000100" 2003  170226643 1  .
    "000100" 2005  278536890 1 30
    "000100" 2002  165529950 1  .
    "000100" 2004  205853387 1 30
    "000100" 2008  296933957 1 30
    "000100" 1994   30913639 1  .
    "000100" 2000  160220765 1  .
    "000100" 2009  283012576 1 30
    "000100" 1999  154610097 1  .
    "000100" 2006  302067287 1 30
    "000100" 1993   25677538 1  .
    "000140" 1997 1253603088 0  .
    "000140" 1996  983906135 0  .
    "000140" 2005 1288155622 0 10
    "000140" 1990  139918401 0  .
    "000140" 1992  239383427 0  .
    "000140" 1999 1159981004 0  .
    "000140" 2008   14067669 0 30
    "000140" 1993  288596685 0  .
    "000140" 2004 1290148390 0 10
    "000140" 2006 1243534175 0 10
    "000140" 2000 1164235987 0  .
    "000140" 1998 1162506847 0  .
    "000140" 2007 1246209909 0 10
    "000140" 1994  382963928 0  .
    "000140" 1991  174445619 0  .
    "000140" 1995  737530402 0  .
    "000140" 2003 1277517059 0  .
    "000140" 2001 1163986025 0  .
    "000140" 2009   29599497 0 10
    "000140" 2002 1232045869 0  .
    "000220" 1992    5523750 1  .
    "000220" 2009   41095464 1 30
    "000220" 2010   47267423 1 30
    "000220" 1991    4285079 1  .
    "000220" 2004   29482372 1 30
    "000220" 2006   52617079 1 30
    "000220" 2007   43958108 1 30
    "000220" 2005   31317282 1 30
    "000220" 1994    6057958 1  .
    "000220" 1993    5772688 1  .
    "000220" 2000   24193849 1  .
    "000220" 2002   24391082 1  .
    "000220" 1995    5918710 1  .
    "000220" 1999    6794837 1  .
    "000220" 1990    3780927 1  .
    "000220" 2003   24806324 1  .
    "000220" 2001   24053777 1  .
    "000220" 1998    7089910 1  .
    "000220" 2008   41701599 1 30
    "000220" 1996    6159711 1  .
    end

  • #2
    A longstanding FAQ https://www.stata.com/support/faqs/d...issing-values/ includes the case of copying backwards in time. The trick is that you must reverse time as otherwise you won't get a cascade of replacements when you need one.

    Comment


    • #3
      Code:
      gsort firmid -year
      by firmid: replace group = group[_n-1] if mi(group) & year<2004

      Comment


      • #4
        Nick Cox , Øyvind Snilsberg Thanks so much.

        Comment

        Working...
        X