This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. [Learn more about bidirectional Unicode characters](https://github.co/hiddenchars) [ Show hidden characters ](<>)
| |
/\* |
|---|---|
| |
Filename: ZIP5\_County\_Lookup.do |
| |
Author: Anthony D'Agostino (ald at satanford dot edu) |
| |
Date Created: 02/09/2018 |
| |
Last Edited: 03/27/2018 |
| |
Purpose: Generate a merged zip5-county crosswalk that leverages the strengths of both the US Census and HUD ZCTA crosswalks. |
| |
|
| |
\*/ |
| | |
| | |
| | clear |
| | clear all |
| | clear matrix |
| | set more off |
| | set maxvar 25000, permanently |
| | |
| | |
| | |
| |
loc basePath
"<your path here>" |
| | |
| | |
| |
// read in county codes, downloaded from: https://www.census.gov/geo/reference/codes/cou.html |
| | import delimited using
"`basePath'/national\_county.txt", delim(
",") clear stringcols(\_all) varnames(nonames) |
| | |
| | ren v1 state |
| | ren v2 state\_fips |
| | ren v3 county\_fips |
| | ren v4 county\_name |
| | ren v5 fipsclasscode |
| | |
| |
gen county
= state\_fips
+ county\_fips |
| | |
| |
tempfile counties |
| | save `counties' |
| | |
| |
\*import excel using "`basePath'/ZIP\_COUNTY\_032011.xlsx", firstrow clear |
| | import excel using
"`basePath'/ZIP\_COUNTY\_122017.xlsx", firstrow clear |
| | |
| |
// 39455 unique entries before any transformations |
| | merge m:1 county using `counties', update replace |
| | tab \_merge |
| | drop if \_merge
== 2 |
| | drop \_merge |
| | |
| | gsort zip
–tot\_ratio |
| |
bys zip:
gen totOrder
= \_n |
| | |
| | gsort zip
–res\_ratio |
| |
bys zip:
gen resOrder
= \_n |
| | |
| | tw (hist res\_ratio if resOrder
== 1), xtitle(
"Residential Address Percent") graphregion(color(white) lwidth(large)) |
| | keep if resOrder
== 1 |
| | |
| |
// some basic cleaning |
| |
replace county\_name
= "Oglala Lakota, SD" if county
== "46102" |
| |
replace state
= "SD" if county
== "46102" |
| |
replace state\_fips
= "46" if county
== "46102" |
| |
replace county\_fips
= "102" if county
== "46102" |
| | |
| |
// see http://www.nws.noaa.gov/om/notification/scn17-57kusilvak\_ak.htm |
| |
replace county\_name
= "Kusilvak Census Area" if county
== "02158" |
| |
replace state
= "AK" if county
== "02158" |
| |
replace state\_fips
= "02" if county
== "02158" |
| |
replace county\_fips
= "158" if county
== "02158" |
| | |
| | drop resOrder totOrder |
| | ren zip zip5 |
| | destring, replace |
| | |
| |
tempfile zip5\_county |
| | save `zip5\_county' |
| | |
| | |
| | import delimited using
"`basePath'/zcta\_county\_rel\_10.txt", delim(
",") clear stringcols(\_all)
// varnames(nonames) |
| | |
| |
\*\* 33120 unique ZCTA5 |
| | ren state state\_fips\_rel |
| | ren county county\_fips\_rel |
| | |
| |
gen county
= state\_fips\_rel
+ county\_fips\_rel |
| | |
| | merge m:1 county using `counties', update replace |
| | tab \_merge |
| | drop if \_merge
== 2 |
| | drop \_merge |
| | |
| | destring, replace |
| | |
| | ren state state\_rel |
| | ren county county\_rel |
| | ren county\_name county\_name\_rel |
| |
gen popabove50
= cond(zpoppct
>= 50, 1, 0) |
| | |
| |
// apportionment: keep county matches with largest pop ZCTA5 share |
| | |
| | gsort zcta5
–poppt |
| |
bys zcta5:
gen popOrder
= \_n |
| | keep if popOrder
== 1 |
| | |
| | ren zpop pop\_zip5 |
| | ren zcta5 zip5 |
| | keep zip5 pop\_zip5 zpoppct county\_rel state\_rel county\_name\_rel state\_fips\_rel county\_fips\_rel |
| | |
| | |
| | merge 1:1 zip5 using `zip5\_county', update replace |
| | tab \_merge |
| | drop \_merge |
| | |
| | |
| |
// prioritize Census values, replace with HUD where missing |
| |
foreach x
in "county" "state\_fips" "county\_fips" { |
| |
gen `x'\_final
= `x'\_rel |
| |
replace `x'\_final
= `x' if `x'\_final
== . |
| | drop `x' |
| | ren `x'\_final `x' |
| | } |
| | |
| |
foreach x
in "county\_name" "state" { |
| |
gen `x'\_final
= `x'\_rel |
| |
replace `x'\_final
= `x' if `x'\_final
== "" |
| | drop `x' |
| | ren `x'\_final `x' |
| | } |
| | |
| | keep zip5 county county\_fips county\_name state state\_fips |
| | save
"`basePath'/ZIP5\_County\_Crosswalk", replace |