Hello Statalist users,
I am working in Stata version 13.1 and am trying to replace missing values with a previous CENSUS year’s value.
My data is panel data from multiple censuses with counties as the groups. However, different censuses are produced at different years. The beale codes and natural amenities data is only completed every 10 years. I would like to copy the census data for Beale codes and amenities factors for years that do not have this data. For example, I would like to copy the data from 1974 to 1978, 1982 to 1987 and so forth. An example of the data I am working with is below:
Using the Stata support documents at:
I found a code in order to replace values with the previous observation after sorting by FIPS and YEAR:
Although this works for 1978 as 1974 would be the only year prior, other years may not work properly. For instance, if 1982 is missing (see the county ALEXANDER with FIPS 17003 in example) then the code below will replace 1987 with 1978 data which is ultimately from 13 years prior in 1974.
Therefore, I would need the code to replace if 1982 was available, but leave blank if 1982 was not available.
Ultimately, due to the nature of this data and its limited change, it may not impact a considerable amount by using a 14 year difference (between 1987 and 1974 for example). However, to be safe, I am really hoping I can get some assistance in trying to figure out a method for the above problem. Thank you for any and all suggestions.
Kind Regards,
Amie
I am working in Stata version 13.1 and am trying to replace missing values with a previous CENSUS year’s value.
My data is panel data from multiple censuses with counties as the groups. However, different censuses are produced at different years. The beale codes and natural amenities data is only completed every 10 years. I would like to copy the census data for Beale codes and amenities factors for years that do not have this data. For example, I would like to copy the data from 1974 to 1978, 1982 to 1987 and so forth. An example of the data I am working with is below:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input int FIPS str9 COUNTY int YEAR byte(BEALE URBAN_INFL) float NATURAL_SCALE byte NATURAL_RANK 17001 "ADAMS" 1974 5 7 -2.1 3 17001 "ADAMS" 1978 . . . . 17001 "ADAMS" 1982 5 7 -2.1 3 17001 "ADAMS" 1987 . . . . 17001 "ADAMS" 1992 5 7 -2.1 3 17001 "ADAMS" 1997 . . . . 17001 "ADAMS" 2002 5 7 -2.1 3 17001 "ADAMS" 2007 . . . . 17001 "ADAMS" 2012 5 7 -2.1 3 17003 "ALEXANDER" 1974 7 8 .95 4 17003 "ALEXANDER" 1978 . . . . 17003 "ALEXANDER" 1987 . . . . 17003 "ALEXANDER" 1992 7 8 .95 4 17003 "ALEXANDER" 1997 . . . . 17003 "ALEXANDER" 2002 7 8 .95 4 17003 "ALEXANDER" 2007 . . . . 17003 "ALEXANDER" 2012 3 8 .95 4 end
HTML Code:
http://www.stata.com/support/faqs/data-management/replacing-missing-values/
Code:
bys FIPS: replace BEALE= BEALE[_n-1] if YEAR==1978
Code:
bys FIPS: replace BEALE= BEALE[_n-1] if YEAR==1987
Ultimately, due to the nature of this data and its limited change, it may not impact a considerable amount by using a 14 year difference (between 1987 and 1974 for example). However, to be safe, I am really hoping I can get some assistance in trying to figure out a method for the above problem. Thank you for any and all suggestions.
Kind Regards,
Amie
Comment