Help! I need somebody! My question is very basic.
Panel data (countries, years). I have a variable, which equals the number of reported victims of human trafficking in each country in year t.
I am trying to generate a new variable, which would equal the average number of victims in country i for years t and t-1. I mean, "victims" in t-1 an in t = the average of "number of victims" in t-1 and in t.
I tried
Code:
sort country_coded year bysort country_coded:gen victims =(Numberofvictimsoftrafficking[_n-1] + Numberofvictimsoftrafficking[_n])/2
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input long country_coded int(year Numberofvictimsoftrafficking) float victims2 1 2008 108 . 1 2009 94 101 1 2010 97 95.5 1 2011 84 90.5 1 2012 92 88 1 2013 95 93.5 2 2003 45 . 2 2004 35 40 2 2005 37 36 2 2006 38 37.5 end label values country_coded country_coded label def country_coded 1 "Albania", modify label def country_coded 2 "Austria", modify
Thanks in advance
0 Response to generate a new variable (average over every two years)
Post a Comment