x cd /pub/data/georef/Tools; %let pgm=zcta_master25; filename pgm "&pgm..sas"; /* The information provided regarding each ZCTA are geographic area codes and names associated with the ZCTA including (up to) two counties, places, county subdivisions, CBSAs, PUMAs and Urbanized Areas, CDs, NECTAS (New England only), CSAs, Metro Divisions, metro type (metro/micro/none) and PctUrban (using 2020 census based defn). Statistical/geographic info include latitude, longitude coordinates, land area in square miles, total pop from the 2020 census (for the entire ZCTA and state/ZCTA) as well as various key demographic indicators from the latest ACS. We are not keeping any MOE (margin of error) estimates for the ACS data on these datasets. Note that we have not included observations related to those areas where ZCTA codes were assigned as 99999, meaning no mail delivery and no ZIP code assigned by the USPS. Such areas typically have very small (usually 0) populations. July 23, 2014: rerunning with latest (vintage 2012) 5-year ACS estimates. Make sure we get the correct primary state and county codes and add State2 variable. August 24, 2023: First run for post-2020 Jan. 9, 2025: bi-annual update from 2023 ACS */ %include "/pub/sas/autoexec.sas"; options nosource mprint notes; libname georef '/pub/data/georef'; libname zctamstr '/home/riceg/work/ZCTAs/zctamstr'; libname mable22 '/pub/data/mable22'; libname dhc2020x '/pub/data/dhc2020x'; %let acsvintage = 2023; %let acsyears = 2019-2023; %libname(acs&acsvintage, opts=); libname acs (acs&acsvintage); libname corrlst '/pub/data/corrlst' access=readonly; filename zip2st '/home/riceg/work/ZCTAs/zip2State20.csv'; options fmtsearch=(mable22.formats work sasctlgs); libname user '/home/riceg/work/ZCTAs/temp'; filename attrib "&pgm..attribs.sas"; title "Creating georef.&pgm with vintage &acsvintage ACS estimates data"; %*include "/pub/data/mable22/sasfmats/urban22.sas"; /*<=========================Comment out===================================== * We generate a set to ID the secondary states associated with ZCTAs. When a ZCTA crosses state lines we want to capture the non-primary state and the 2020 population of that piece. Sometimes these pieces can be substantial, as with 58439 (53/47 break between North and South Dakota). The file we read in comes from geocorr22. ; data zcta_splitstates (label='ZCTAs that cross state lines, per 2020 census' index=(zcta5)); infile zip2st dsd firstobs=2 ; length zcta5 $5 State $2 Stab $2 ZIPname $60 pop20zcta_st 5 afact 4; input zcta5--afact; if afact < 1; format afact 5.3; run; proc sort force; by zcta5 afact; run; * Collapse into 1 obs per split ZCTA with State2 and PctState2 variables added ; data user.zcta_splitstates(label="ZCTAs that cross state boundaries, per 2020 census" index=(state)); set; by zcta5; length PctState PctState2 4; format Pct: 5.1; label pctstate = '% pop in state' pctstate2 = '% pop in state2' pop20zcta_st = '2020 pop in ZCTA/state'; if first.zcta5 then do; State2 = State; Stab2 = stab; Pop20State2 = pop20zcta_st; PctState2 = 100 * afact; retain State2 Stab2 Pop20State2 PctState2; delete; end; * If we get here, we are looking at the 2nd obs for the zcta, the one with the majority of pop. ; PctState = 100 * afact; drop afact; output; run; ===========================End commented out================================ */ /*<=========================Comment out===================================== * NO LONGER NECESSARY? We discovered some problems with the state/stab variables on the acs.uszctas5yr dataset that can be traced back to the allgeos dataset (e.g., for 58439, the state was 38/ND when it should have been 46/ND). So we have to apply some patches. We begin by identifying the scope of the problem and generating a potential updates data set. ; proc sort data=acs.uszctas5yr(keep=zcta5 state stab) out=user.acszctastates; by zcta5; run; data user.allgeos_state_updates(label="See &pgm for details"); merge user.acszctastates (in=inacs rename=(state=oldstate stab=oldstab)) user.zcta_splitstates(in=insplits); by zcta5; if insplits and (oldstate ne state) and (zcta5 ne '99999'); run; ===========================End commented out================================ */ /*<=========================Comment out===================================== */ %macro doit(state, stab); %let ustab = %upcase(&stab); %* This code to special process New England added 8-15-13 ; %if %index(CT ME MA NH RI VT,&ustab) %then %do; %let nectavars = necta nectadiv cnecta; %let nectatyps = nec necdiv cnec ; %end; %else %do; %let nectavars = ; %let nectatyps = ; %end; proc sort data=mable22.&stab.v(keep=ZCTA County CouSub20 Place pop20 intptLat intptLon LandSQMI ur ua cd118 puma12 puma22 state &nectavars CBSA20 Metdiv20 cbsatype20 csa20) out=user.sortedcodes; by zcta county; run; data user.geocodes; set user.sortedcodes; length gtype $6 geocode $5; array geocodes(*) county Cousub20 place ur ua cd118 puma12 puma22 &nectavars cbsa20 metdiv20 csa20 cbsatype20; retain _gtypes "cou mcd pla ur ua cd puma12 puma22 &nectatyps cbsa metdiv csa cbsat "; do _i = 1 to dim(geocodes); geocode = geocodes{_i}; gtype = scan(_gtypes,_i); output; end; keep state zcta gtype geocode pop20; rename zcta = zcta5; run; %corrwt( setin=user.geocodes, setout=user.&stab.corrwt, g1=state zcta5 gtype, g2=geocode, wtvar=pop20, keepwt=0 ); title2 "&stab.corrwt"; run ; data user.&stab.data; set user.&stab.corrwt; by state zcta5 gtype ; where zcta5 ne '99999'; length ZIPName $40; if first.zcta5 then ZIPName = put(zcta5, $zipname.); retain ZIPName; retain; length County County_2 Place Place_2 $5 CouSub20 CouSub20_2 $5 cd118 cd118_2 $2 puma12 puma22 $5 necta nectaDiv $5 Cnecta $3 cbsa20 $5 MetDiv20 $5 csa20 $3 CBSAType20 $5 ; if first.zcta5 then do; array geos(*) County -- CBSAType20; do i = 1 to dim(geos); geos(i) = ' '; end; PctUrban = 0; end; if first.gtype then _ng = 1; else _ng+1; if _ng > 2 then goto cklast; select(gtype); when('cou') do; if _ng = 1 then do; County = geocode; pctcnty = 100 * afact; pctcnty_2 = .; end; else do; County_2 = Geocode; pctcnty_2 = 100 * afact; end; end; when('ur') do; if geocode='U' then PctUrban = 100 * afact; else pcturban = 100 * (1 - afact); end; when('mcd') do; if _ng = 1 then do; cousub20 = geocode; pctcousub20 = 100 * afact; end; else do; cousub20_2 = geocode; end; end; when('pla') do; if _ng = 1 then do; place = geocode; pctplace = 100 * afact; pctplace_2 = .; end; else do; place_2 = geocode; pctplace_2 = 100 * afact; end; end; when('ua ') do; if _ng = 1 then do; ua = geocode; pctua = 100 * afact; end; if _ng = 2 and ua = ' ' then do; ua = geocode; pctua = 100 * afact; end; * Special case: we report the non-blank Urbanized Area code even if it is not the largest ; end; when('cd ') do; if _ng = 1 then do; cd118 = geocode; pctcd118 = 100 * afact; end; else do; cd118_2 = geocode; end; end; when('puma12') do; if _ng = 1 then do; puma12 = geocode; pctpuma12 = 100 * afact; end; end; when('puma22') do; if _ng = 1 then do; puma22 = geocode; pctpuma22 = 100 * afact; end; end; when('nec') do; if _ng = 1 then do; necta = geocode; pctnecta = 100 * afact; end; end; when('necdiv') do; if _ng = 1 then do; nectadiv = geocode; pctnectadiv = 100 * afact; end; end; when('cnec') do; if _ng = 1 then do; cnecta = geocode; pctcnecta = 100 * afact; end; end; when('cbsa') do; if _ng = 1 then do; cbsa20 = geocode; pctcbsa20 = 100 * afact; end; end; when('csa') do; if _ng = 1 then do; csa20 = geocode; pctcsa20 = 100 * afact; end; end; when('metdiv') do; if _ng = 1 then do; metdiv20 = geocode; pctmetdiv20 = 100 * afact; end; end; when('cbsat') do; if _ng = 1 then do; cbsatype20 = geocode; pctcbsatype20 = 100 * afact; end; end; end; * of select (gtype) ; cklast: if last.zcta5 then do; Fipco = county; FipCo2 = County_2; length PlaceName $7; if Place ne '99999' then PlaceName = state||Place; else PlaceName=' '; CBSAName = cbsa20; MetDivName = MetDiv20; CSAName = csa20; NectaName = necta; length UAName $96 Puma22Name $80; length _arg $7; _arg = state||puma22; Puma22Name = put(_arg, $puma22_.); %local i prefix pfxs; %let pfxs = MetDiv CSA UA Place Necta; %let i = 1; %let prefix = %scan(&pfxs, &i); %do %until(&prefix eq %str()); if &prefix.Name =: '99999' then &prefix.Name = ' '; %let i = %eval(&i + 1); %let prefix = %scan(&pfxs, &i); %end; format County County_2 $county. PlaceName $place20_. CBSAName $cbsa20_. nectaname $necta. MetDivname $metdiv20_. csaname $csa21_. PUMA22Name $puma22_. UAName $urban22_. ; format Pct: 5.1; output; end; drop gtype geocode afact i _: ; run; proc print data=&syslast; title2 "&syslast"; by state; id zcta5; run cancel; * The ACS ZCTA data is for complete ZCTAs (not within state!). The State variable that we added is just the "primary" state. Which is not so good for us because we would prefer to build a data set that is state/ZCTA based. But... ; data user.&stab.acsdata; set acs.uszctas5yr(keep=geoid state zcta5 Areaname stab PctState SumLev IntPtLat Intptlon Landsqmi AreaSQMI TotPop MedianAge pctUnder18 pctOver65 pctwhite1 pctblack1 pctasian1 pctHispanicPop TotHHs MedianHHInc FamHHs MedianFamInc PovUniverse PctPoor pctGrpQuarters pctBachelorsormore pctInCollege pctForeignBorn TotHUs OccHUs pctRenterOcc MedianHValue MedianGrossRent Where=(state="&state" and zcta5 ne '99999' and TotPop) ); if zcta5 ne ' '; ACSYears = "&acsyears"; format AreaSQMI LandSQMI 9.2 ; rename TotPop=TotPopACS; label TotPop="Total population, ACS &acsyears period estimates"; run; proc sort data=corrlst.zip_2_zcta20(where=(stab="&ustab" and (zip ne zcta)) drop=ZIPType CityName) out=user.altzips; by zcta; run; data user.altzips2; set user.altzips; by zcta; length AltZIPs $356 NaltZIPs 3; retain State "&state" AltZIPs; if first.zcta then do; AltZIPS = ZIP; Naltzips = 1; end; else do; naltzips+1; AltZIPs = trim(altZIPS)||' '||zip; end; if last.zcta then output; keep state zcta naltzips altzips; rename zcta=zcta5; run; data zctamstr.&stab.zips(label="&statenm ZCTA Master reference" compress=yes sortedby=state zcta5); %include attrib/NOsource2; merge user.&stab.data (in=in1) corrlst.us_zip2020_state(in=inzpst keep=zcta5 state nparts primary Pop2020 ZipTotPop pctpop rename=(nparts=Nstates primary=psf pctpop=pctState Pop2020=Pop20ZCTAstate)) user.zcta_splitstates(where=(state="&state") keep=state zcta5 state2 stab2 PctState PctState2 Pop20State2 in=insplit) altzips2(in=inaltzips) user.&stab.acsdata(in=inacs) ; by state zcta5; if first.state then do; length _divreg $2 Div Reg $1; _divreg = put(state, $stdivr.); Div = substr(_divreg, 1, 1); Reg = substr(_divreg, 2, 1); retain Reg Div; end; if not inacs then delete; label ZIPTotPop='ZCTA 2020 total across all states'; label Reg='Census Region' div='Census Division'; if div ne '1' then do; * set all New England specific items to missing for states outside NE (Div 1) ; necta = ' '; nectaname = ' '; nectadiv = ' '; cnecta = ' '; pctnecta = .; pctnectadiv = .; end; if not (in1 and inacs and inzpst) then do; put 'Not on all datasets: ' state= zcta5= in1= inacs= inzpst= inaltzips=; if in1; end; if not inaltzips then Naltzips = 0; format placename $place20_. cbsaname $cbsa20_. csaname $csa21_. ; drop _: ; run; %mend doit; %dostates(10, names=1); %*dostates(n=52, msgs=1, names=1) ; %*genattr(setin=user.dezips); %* (optional) generate original template then edit later and include in convert step--; %macro setref(state,stab); zctamstr.&stab.zips %mend setref; data user.zcta_master_2025(label='2025 vintage ZCTA Master for U.S.' index=(state)); set %*dostates(n=52, mac=setref) open=defer; %dostates(10, mac=setref) open=defer; run; /* ===========================End commented out================================ */