-
Notifications
You must be signed in to change notification settings - Fork 3
/
countyfips.ado
151 lines (134 loc) · 4.84 KB
/
countyfips.ado
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
countyfips: Stata program for merging U.S. county identifiers
Author: Christopher B. Goodman
Contact: [email protected]
Date: NA
Version: 0.9.1
*/
capture program drop countyfips
program define countyfips
version 12.1
syntax, [Name(string) Statefips(string) Countyfips(string) Fips(string) Statecode(string) Countycode(string) NOGENerate]
cap quietly findfile countyfips.dta, path("`c(sysdir_personal)'countyfips_data/")
if _rc==601 {
preserve
clear
quietly findfile countyfips_data.ado
cap insheet using "`r(fn)'", tab
label var fips "5-digit FIPS Code"
label var county_name "County Name"
label var state_abb "State Abbreviation"
label var state_fips "State FIPS Code"
label var county_fips "County FIPS Code"
label var state_code "State Census Code"
label var county_code "County Census Code"
cap mkdir "`c(sysdir_personal)'"
cap mkdir "`c(sysdir_personal)'countyfips_data"
cap save "`c(sysdir_personal)'countyfips_data/countyfips.dta"
restore
}
if "`nogenerate'" != "" {
if "`name'" != "" & "`statefips'" != "" {
local nm "`name'"
local st "`statefips'"
rename `nm' county_name
rename `st' state_fips
replace county_name=upper(county_name)
merge m:1 county_name state_fips using "`c(sysdir_personal)'countyfips_data/countyfips.dta", nogen keep(match master)
rename county_name `nm'
rename state_fips `st'
}
else if "`name'" != "" & "`statecode'" != "" {
local nm "`name'"
local statecode "`statecode'"
rename `nm' county_name
rename `statecode' state_code
replace county_name=upper(county_name)
merge m:1 county_name state_code using "`c(sysdir_personal)'countyfips_data/countyfips.dta", nogen keep(match master)
rename county_name `nm'
rename state_code `statecode'
}
else if "`fips'" != "" {
local fips "`fips'"
rename `fips' fips
merge m:1 fips using "`c(sysdir_personal)'countyfips_data/countyfips.dta", nogen keep(match master)
rename fips `fips'
}
else if "`statefips'" != "" & "`countyfips'" != "" {
local statefips "`statefips'"
local countyfips "`countyfips'"
rename `statefips' state_fips
rename `countyfips' county_fips
merge m:1 state_fips county_fips using "`c(sysdir_personal)'countyfips_data/countyfips.dta", nogen keep(match master)
rename state_fips `statefips'
rename county_fips `countyfips'
}
else if "`statecode'" != "" & "`countycode'" != "" {
preserve
clear
cap quietly use "`c(sysdir_personal)'countyfips_data/countyfips.dta"
quietly drop if county_code==.
tempfile countyfips_datatemp
cap save `countyfips_datatemp'
restore
local statecode "`statecode'"
local countycode "`countycode'"
rename `statecode' state_code
rename `countycode' county_code
merge m:1 state_code county_code using `countyfips_datatemp', nogen keep(match master)
rename state_code `statecode'
rename county_code `countycode'
}
}
else if "`name'" != "" & "`statefips'" != "" {
local nm "`name'"
local st "`statefips'"
rename `nm' county_name
rename `st' state_fips
replace county_name=upper(county_name)
merge m:1 county_name state_fips using "`c(sysdir_personal)'countyfips_data/countyfips.dta"
rename county_name `nm'
rename state_fips `st'
}
else if "`name'" != "" & "`statecode'" != "" {
local nm "`name'"
local statecode "`statecode'"
rename `nm' county_name
rename `statecode' state_code
replace county_name=upper(county_name)
merge m:1 county_name state_code using "`c(sysdir_personal)'countyfips_data/countyfips.dta"
rename county_name `nm'
rename state_code `statecode'
}
else if "`fips'" != "" {
local fips "`fips'"
rename `fips' fips
merge m:1 fips using "`c(sysdir_personal)'countyfips_data/countyfips.dta"
rename fips `fips'
}
else if "`statefips'" != "" & "`countyfips'" != "" {
local statefips "`statefips'"
local countyfips "`countyfips'"
rename `statefips' state_fips
rename `countyfips' county_fips
merge m:1 state_fips county_fips using "`c(sysdir_personal)'countyfips_data/countyfips.dta"
rename state_fips `statefips'
rename county_fips `countyfips'
}
else if "`statecode'" != "" & "`countycode'" != "" {
preserve
clear
cap quietly use "`c(sysdir_personal)'countyfips_data/countyfips.dta"
quietly drop if county_code==.
tempfile countyfips_datatemp
cap save `countyfips_datatemp'
restore
local statecode "`statecode'"
local countycode "`countycode'"
rename `statecode' state_code
rename `countycode' county_code
merge m:1 state_code county_code using `countyfips_datatemp'
rename state_code `statecode'
rename county_code `countycode'
}
end