Dear Statalist
I am using Stata 15 and I am trying to replace some missing values for a period of three years previous to a given year (this year is not fixed). For example, for the variable (x1) for id=1 the years 1990-1992 are missing and I need to replace it by the value in 1993. Again, I need to replace the missing in 1994-1996 by the value in 1997, and so on until 2013. I am using the following command:
I am pretty sure that it might be an easier, shorter and more eficient way to do it. However, I do not know how to do it, and with the command I show you above I cannot see why is it failing.
The first three years for the variable “x1” should have the value 1 (coming from 1993) but instead it put a “2”. It is clear that I am doing something wrong but I cannot figure out what it is.
Here you have an example of the dataset:
Any help will be much appreciated.
I am using Stata 15 and I am trying to replace some missing values for a period of three years previous to a given year (this year is not fixed). For example, for the variable (x1) for id=1 the years 1990-1992 are missing and I need to replace it by the value in 1993. Again, I need to replace the missing in 1994-1996 by the value in 1997, and so on until 2013. I am using the following command:
Code:
gen q1 = . replace q1 = 1 if year==1990 | year==1991 | year==1992 gen q2 = . replace q2 = 1 if year==1994 | year==1995 | year==1996 gen q3 = . replace q3 = 1 if year==1998 | year==1999 | year==2000 gen q4 = . replace q4 = 1 if year==2002 | year==2003 | year==2004 gen q5 = . replace q5 = 1 if year==2006 | year==2007 | year==2008 gen q6 = . replace q6 = 1 if year==2010 | year==2011 | year==2012 global quarter " x1 x2 x3 x4 " global time " q1 q2 q3 q4 q5 q6 " foreach x in $quarter { foreach y in $time { forvalues i = 1993(4)2013 { egen `x'_`y' = mean(`x') if year == `i', by(id) egen `x'_`y'_mode = mode( `x'_`y' ), by(id) replace `x' = `x'_`y'_mode if `y' == 1 drop `x'_`y' `x'_`y'_mode } } }
The first three years for the variable “x1” should have the value 1 (coming from 1993) but instead it put a “2”. It is clear that I am doing something wrong but I cannot figure out what it is.
Here you have an example of the dataset:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input int(id year) byte x1 int(x2 x3 x4) 1 1990 . . . . 1 1991 . . . . 1 1992 . . . . 1 1993 1 5 1 1 1 1994 . . . . 1 1995 . . . . 1 1996 . . . . 1 1997 2 7 1 1 1 1998 . . . . 1 1999 . . . . 1 2000 . . . . 1 2001 2 3 3 1 1 2002 . . . . 1 2003 . . . . 1 2004 . . . . 1 2005 2 5 2 1 1 2006 . . . . 1 2007 . . . . 1 2008 . . . . 1 2009 2 2 3 1 1 2010 . . . . 1 2011 . . . . 1 2012 . . . . 1 2013 2 1 6 1 1 2014 2 2 6 0 1 2015 2 2 6 0 2 1990 . . . . 2 1991 . . . . 2 1992 . . . . 2 1993 2 0 2 0 2 1994 . . . . 2 1995 . . . . 2 1996 . . . . 2 1997 2 0 0 0 2 1998 . . . . 2 1999 . . . . 2 2000 . . . . 2 2001 1 0 1 3 2 2002 . . . . 2 2003 . . . . 2 2004 . . . . 2 2005 2 2 6 3 2 2006 . . . . 2 2007 . . . . 2 2008 . . . . 2 2009 . . . . 2 2010 . . . . 2 2011 . . . . 2 2012 . . . . 2 2013 . . . . 2 2014 . . . . 2 2015 . . . . 3 1990 . . . . 3 1991 . . . . 3 1992 . . . . 3 1993 2 6 1 4 3 1994 . . . . 3 1995 . . . . 3 1996 . . . . 3 1997 2 1 0 4 3 1998 . . . . 3 1999 . . . . 3 2000 . . . . 3 2001 2 1 0 2 3 2002 . . . . 3 2003 . . . . 3 2004 . . . . 3 2005 1 2 0 3 3 2006 . . . . 3 2007 . . . . 3 2008 . . . . 3 2009 1 2 0 3 3 2010 . . . . 3 2011 . . . . 3 2012 . . . . 3 2013 2 2 0 3 3 2014 . . . . 3 2015 . . . . 4 1990 . . . . 4 1991 . . . . 4 1992 . . . . 4 1993 2 0 0 0 4 1994 . . . . 4 1995 . . . . 4 1996 . . . . 4 1997 2 0 0 0 4 1998 . . . . 4 1999 . . . . 4 2000 . . . . 4 2001 . . . . 4 2002 . . . . 4 2003 . . . . 4 2004 . . . . 4 2005 . . . . 4 2006 . . . . 4 2007 . . . . 4 2008 . . . . 4 2009 . . . . 4 2010 . . . . 4 2011 . . . . 4 2012 . . . . 4 2013 . . . . 4 2014 . . . . 4 2015 . . . . end
Any help will be much appreciated.
Comment