所以我有一个按年和(美国)州确定的观察数据集,然后我collapse
下降到Census division的水平。但是,当我在人口普查逐个数据中尝试merge
时,我收到variable not found r(111)
错误。更具体地说:
///////////////// Generate Master Data ///////////////
clear
input str20 state int year byte fips long popstatecensus float circ_per_cap str18 census_division byte census_division_num
"Florida" 1995 12 14537875 . "South Atlantic" 5
"Nevada" 1998 32 1853191 . "Mountain" 8
"Louisiana" 1989 22 4252894 . "West South Central" 7
"West Virginia" 2005 54 1820492 .004683349 "South Atlantic" 5
"Nevada" 1997 32 1764104 . "Mountain" 8
"Idaho" 2001 16 1319962 . "Mountain" 8
"New Hampshire" 2006 33 1308389 .0037779284 "New England" 1
"Texas" 2001 48 21319622 . "West South Central" 7
"Idaho" 1990 16 1012384 . "Mountain" 8
"Illinois" 1977 17 11386316 . "East North Central" 3
"Nevada" 1988 32 1075022 . "Mountain" 8
"Kentucky" 2002 21 4089875 . "East South Central" 6
end
//////////////// Collapse to Census Division Level ///////////////
collapse (mean) circ_per_cap (rawsum) popstatecensus [aweight = popstatecensus], by(year census_division)
save master_dataset, replace
//////////////////// Generate Using Data /////////////////////////
clear
input int year byte census_division_num float(est_personal_rate est_household_rate est_handgun_rate est_shotgun_rate est_rifle_rate est_longgun_rate) str18 census_division float random
1991 7 39.98576 54.11765 31.764706 35.294117 38.82353 47.05882 "West South Central" .010295177
2010 9 17.692259 24.590164 14.689265 15.254237 14.689265 19.77401 "Pacific" .012932513
1996 5 33.69932 45.60907 27.142857 27.714285 24.285715 35.42857 "South Atlantic" .016487759
2010 3 17.774584 32.608696 16.666666 21.75926 18.981482 26.38889 "East North Central" .017706182
1974 4 . 45.26316 17.894737 34.736843 30.526316 43.15789 "West North Central" .019860983
1994 2 18.623333 23.29749 11.15108 16.906475 18.345324 20.86331 "Middle Atlantic" .023200603
1987 1 5.904935 22.07792 10.38961 12.987013 14.285714 16.883118 "New England" .02847081
1994 5 31.804846 43.29897 28.83117 25.454546 21.2987 30.64935 "South Atlantic" .03338598
1987 4 29.90744 52.8 17.6 40 34.4 46.4 "West North Central" .034379292
1977 7 . 50.42017 28.57143 31.932774 29.411764 39.4958 "West South Central" .0425348
1988 9 24.669197 40.17857 26.85185 16.666666 24.074074 31.48148 "Pacific" .04586423
2012 9 17.482399 25.25773 18.848167 12.041884 14.659686 18.848167 "Pacific" .04700154
end
sort census_division year
save using_dataset, replace
//// Attempt Merge ////
use master_dataset, clear
merge 1:1 census_division year using using_dataset
抛出r(111)
错误。变量census_division
确实存在于主数据集和使用数据集中,并且不应该出现tsset
的任何问题,因为我在折叠之前清除了它。
感谢您的帮助!