I have data on migration numbers between countries and their neighbors. The issue is that the data is recorded for only every fifth year since 1990. So there is data for only 1990, 1995, 2000, etc up to 2019. I want to fill in for the years in between, using the average of the two years available from below and above. So for 1991, 1992, 1993, and 1994, I want to use the average of 1990 and 1995 values. And this has to be grouped by country and neighbor.
The original variable is migrant_stock. the following code is the idea, not a real code
clonevar emigrants = migrant_stock
bysort country neighbor year : replace ///
missing(emigrants from 1991 - 1994) = mean(emigrants of 1990 and 1995) ///
missing(emigrants from 1996 - 1999) = mean(emigrants of 1995 and 2000) ///
missing(emigrants from 2001 - 2004) = mean(emigrants of 2000 and 2005) ///
missing(emigrants from 2006 - 2009) = mean(emigrants of 2005 and 2010) ///
missing(emigrants from 2011 - 2004) = mean(emigrants of 2010 and 2015) ///
missing(emigrants from 2016 - 2018) = mean(emigrants of 2015 and 2019)
* Example generated by -dataex-. To install: ssc install dataex
clear
input str59 country str28 neighbor int year long(migrant_stock emigrants)
"Algeria" "Libya" 1990 1982 1982
"Algeria" "Libya" 1991 . .
"Algeria" "Libya" 1992 . .
"Algeria" "Libya" 1993 . .
"Algeria" "Libya" 1994 . .
"Algeria" "Libya" 1995 2154 2154
"Algeria" "Libya" 1996 . .
"Algeria" "Libya" 1997 . .
"Algeria" "Libya" 1998 . .
"Algeria" "Libya" 1999 . .
"Algeria" "Libya" 2000 2325 2325
"Algeria" "Libya" 2001 . .
"Algeria" "Libya" 2002 . .
"Algeria" "Libya" 2003 . .
"Algeria" "Libya" 2004 . .
"Algeria" "Libya" 2005 1835 1835
"Algeria" "Libya" 2006 . .
"Algeria" "Libya" 2007 . .
"Algeria" "Libya" 2008 . .
"Algeria" "Libya" 2009 . .
"Algeria" "Libya" 2010 2016 2016
"Algeria" "Libya" 2011 . .
"Algeria" "Libya" 2012 . .
"Algeria" "Libya" 2013 . .
"Algeria" "Libya" 2014 . .
"Algeria" "Libya" 2015 2225 2225
"Algeria" "Libya" 2016 . .
"Algeria" "Libya" 2017 . .
"Algeria" "Libya" 2018 . .
"Algeria" "Libya" 2019 2314 2314
"Algeria" "Tunisia" 1990 . .
"Algeria" "Tunisia" 1991 . .
"Algeria" "Tunisia" 1992 . .
"Algeria" "Tunisia" 1993 . .
"Algeria" "Tunisia" 1994 . .
"Algeria" "Tunisia" 1995 . .
"Algeria" "Tunisia" 1996 . .
"Algeria" "Tunisia" 1997 . .
"Algeria" "Tunisia" 1998 . .
"Algeria" "Tunisia" 1999 . .
"Algeria" "Tunisia" 2000 . .
"Algeria" "Tunisia" 2001 . .
"Algeria" "Tunisia" 2002 . .
"Algeria" "Tunisia" 2003 . .
"Algeria" "Tunisia" 2004 . .
"Algeria" "Tunisia" 2005 . .
"Algeria" "Tunisia" 2006 . .
"Algeria" "Tunisia" 2007 . .
"Algeria" "Tunisia" 2008 . .
"Algeria" "Tunisia" 2009 . .
"Algeria" "Tunisia" 2010 . .
"Algeria" "Tunisia" 2011 . .
"Algeria" "Tunisia" 2012 . .
"Algeria" "Tunisia" 2013 . .
"Algeria" "Tunisia" 2014 . .
"Algeria" "Tunisia" 2015 . .
"Algeria" "Tunisia" 2016 . .
"Algeria" "Tunisia" 2017 . .
"Algeria" "Tunisia" 2018 . .
"Algeria" "Tunisia" 2019 . .
"Angola" "Congo, Dem. Rep." 1990 12178 12178
"Angola" "Congo, Dem. Rep." 1991 . .
"Angola" "Congo, Dem. Rep." 1992 . .
"Angola" "Congo, Dem. Rep." 1993 . .
"Angola" "Congo, Dem. Rep." 1994 . .
"Angola" "Congo, Dem. Rep." 1995 14957 14957
"Angola" "Congo, Dem. Rep." 1996 . .
"Angola" "Congo, Dem. Rep." 1997 . .
"Angola" "Congo, Dem. Rep." 1998 . .
"Angola" "Congo, Dem. Rep." 1999 . .
"Angola" "Congo, Dem. Rep." 2000 17735 17735
"Angola" "Congo, Dem. Rep." 2001 . .
"Angola" "Congo, Dem. Rep." 2002 . .
"Angola" "Congo, Dem. Rep." 2003 . .
"Angola" "Congo, Dem. Rep." 2004 . .
"Angola" "Congo, Dem. Rep." 2005 24340 24340
"Angola" "Congo, Dem. Rep." 2006 . .
"Angola" "Congo, Dem. Rep." 2007 . .
"Angola" "Congo, Dem. Rep." 2008 . .
"Angola" "Congo, Dem. Rep." 2009 . .
"Angola" "Congo, Dem. Rep." 2010 30944 30944
"Angola" "Congo, Dem. Rep." 2011 . .
"Angola" "Congo, Dem. Rep." 2012 . .
"Angola" "Congo, Dem. Rep." 2013 . .
"Angola" "Congo, Dem. Rep." 2014 . .
"Angola" "Congo, Dem. Rep." 2015 77548 77548
"Angola" "Congo, Dem. Rep." 2016 . .
"Angola" "Congo, Dem. Rep." 2017 . .
"Angola" "Congo, Dem. Rep." 2018 . .
"Angola" "Congo, Dem. Rep." 2019 92242 92242
"Angola" "Congo, Rep." 1990 410 410
"Angola" "Congo, Rep." 1991 . .
"Angola" "Congo, Rep." 1992 . .
"Angola" "Congo, Rep." 1993 . .
"Angola" "Congo, Rep." 1994 . .
"Angola" "Congo, Rep." 1995 477 477
"Angola" "Congo, Rep." 1996 . .
"Angola" "Congo, Rep." 1997 . .
"Angola" "Congo, Rep." 1998 . .
"Angola" "Congo, Rep." 1999 . .
end
[/CODE]
------------------ copy up to and including the previous line ------------------
Thank you for your assistance.
0 Response to Missing value fill-in with data from specific dates
Post a Comment