x cd /home/riceg/work/ZCTAs; %let pgm=zcta_master20; 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 2010 census based defn). Statistical/geographic info include latitude, longitude coordinates, land area in square miles, total pop from the 2010 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. jgb, September, 2012. Modeled after original setup that created a comparable zcta_master dataset with 2000-vintage geos/data. Aug, 2013: Rerun with 2007-2011 ACS data, latest mable14 data and with a much longer AltZIPs variable. Aug 29, 2013: running after updating mable14.XXv views to get latest cbsa, metdiv, csa and metrotype. Sept 4, 2013: adding the confusing and horrible Pop10ZCTAstate variable with the pop of just the state portion of the ZCTA. 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. */ %include "/pub/sas/autoexec.sas"; libname georef '/pub/data/georef'; libname zctamstr '/home/riceg/work/ZCTAs/zctamstr'; libname mable18 '/pub/data/mable18'; libname sf12010x '/pub/data/sf12010x'; %let acsvintage=2018; %let acsvintagem4=%eval(&acsvintage - 4); %let acsyears=&acsvintagem4.-&acsvintage; %libname(acs&acsvintage,opts=); libname acs (acs&acsvintage); libname corrlst '/pub/data/corrlst' access=readonly; filename zip2st '/home/riceg/work/ZCTAs/zip2State.csv'; options fmtsearch=(mable18 work sasctlgs); *<=====; libname user '/home/riceg/work/ZCTAs/temp'; filename attrib "&pgm..attribs.sas"; title "Creating georef.&pgm with Vintage &acsvintage ACS Estimates Data"; /*<=========================Comment out=====================================*/ *-- New for the 2014 conversion run (using 2012 vintage ACS data) we generate a set to ID the secondary states associated with ZCTAs, i.e. when a ZCTA crosses state lines we want to capture the non-primary state and the 2010 pop of that "sliver". Sometimes these slivers can be substantial, as with 58439 (53/47 break between North and South Dakota). The file we read in came from geocorr 2000. --; data zcta_splitstates; ** (label='ZCTAs that Cross State Lines Per 2010 Census' index=(zcta5)); infile zip2st dsd firstobs=3 ; length zcta5 $5 State $2 Stab $2 ZIPname $40 pop10zcta_st 5 afact 4; input zcta5--afact; if afact < 1; format afact 5.3; run; *---Collapse into 1 obs per split ZCTA with State2 and PctState2 variables added--; proc sort; by zcta5 afact; data user.zcta_splitstates(label="ZCTAs that cross state boundaries per 2010 Census" index=(state)); set; by zcta5; length PctState PctState2 4; format Pct: 5.1; label pctstate='% Pop in State' pctstate2='% Pop in State2'; label pop10zcta_st= '2010 Pop in ZCTA5/State'; if first.zcta5 then do; State2=State; Stab2=stab; Pop10State2=pop10zcta_st; PctState2=100*afact; retain State2 Stab2 Pop10State2 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================================ */ *---We discovered some problems with the state/stab variables on the acs.uszctas5yr data set that can be traced back to the allgeos2008_2012 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.allgeos2014_2018(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'); *--we found that only 2 ZCTAs were a problem: 58439 and 97635 SO WE USED FSEDIT TO FIX 2 OBS ON ACS.USZCTAS5YR--; run; %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; %*--it is in New England--; %let nectavars=necta nectadiv cnecta; %let nectatyps=nec necdiv cnec ; %end; %else %do; %let nectavars=; %let nectatyps= ; %end; proc sort data=mable18.&stab.v(keep=ZCTA5 County CouSubFP PlaceFP pop10 intptLat intptLon LandSQMI ur ua cd113 puma2k puma12 state &nectavars CBSA Metdiv cbsatype csa)out=user.sortedcodes; by zcta5 county; run; data user.geocodes; set user.sortedcodes; length gtype $6 geocode $5; array geocodes(*) county Cousubfp placefp ur ua cd113 puma2k puma12 cnty2k &nectavars cbsa metdiv csa cbsatype; retain _gtypes "cou mcd pla ur ua cd puma2k puma12 co2 &nectatyps cbsa metdiv csa cbsat "; do _i=1 to dim(geocodes); geocode=geocodes{_i}; gtype=scan(_gtypes,_i); output; end; keep state zcta5 gtype geocode pop10; run; %corrwt(setin=user.geocodes,setout=user.&stab.corrwt,g1=state zcta5 gtype,g2=geocode,wtvar=pop10,keepwt=0); /* proc sort; by state zcta gtype descending afact; */ 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 County2 PlaceFP PlaceFP2 $5 CouSubFP CouSubFP2 $5 cd113 cd113_2 $2 puma2k puma12 $5 necta nectaDiv $5 Cnecta $3 cbsa $5 MetDiv $5 csa $3 CBSAType $5 ; if first.zcta5 then do; array geos(*) County -- CBSAType; 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; pctcnty2=.; end; else do; County2=Geocode; pctcnty2=100*afact; end; end; when('co2') do; if _ng=1 then do; cnty2k=geocode; pctcnty2k=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; cousubfp=geocode; pctcousub=100*afact; end; else do; cousubfp2=geocode; end; end; when('pla') do; if _ng=1 then do; placefp=geocode; pctplace=100*afact; pctplace2=.; end; else do; placefp2=geocode; pctplace2=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; cd113=geocode; pctcd113=100*afact; end; else do; cd113_2=geocode; end; end; *when('msa') do; * if _ng=1 then do; * msacmsa=geocode; * pctmsacmsa=100*afact; * end; * else if msacmsa='9999' then do; * msacmsa=geocode; * pctmsacmsa=100*afact; * end; * end; when('puma2k') do; if _ng=1 then do; puma2k=geocode; pctpuma2k=100*afact; end; end; when('puma12') do; if _ng=1 then do; puma12=geocode; pctpuma12=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; cbsa=geocode; pctcbsa=100*afact; end; end; when('csa') do; if _ng=1 then do; csa=geocode; pctcsa=100*afact; end; end; when('metdiv') do; if _ng=1 then do; metdiv=geocode; pctmetdiv=100*afact; end; end; when('cbsat') do; if _ng=1 then do; cbsatype=geocode; pctcbsatype=100*afact; end; end; **otherwise ; end; *---select (gtype)---; cklast: if last.zcta5 then do; Fipco=county; FipCo2=County2; length PlaceName $7; if PlaceFP ne '99999' then PlaceName=state||PlaceFP; else PlaceName=' '; CBSAName=cbsa; MetDivName=MetDiv; CSAName=csa; NectaName=necta; *---replace this code that requires accessing mable14 format codes with storing actual names--; ***UAName=ua; ***length Puma12Name $7; ***puma12Name=state||puma12; length UAName $96 Puma12Name $80; UAName=put(ua,$uauc.); length _arg $7; _arg=state||puma12; Puma12Name=put(_arg,$puma.); %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; length UAtype $2; if ua=' ' then UAType=' '; else do; *--replacing old code with new here, 8-13. Old code commented out.------*; **_uaname=put(ua,$uauc2k.); **if scan(_uaname,-1,' ')='(UA)' then uatype='UA'; ** else uatype='UC'; *--urban cluster or urbanized area?--; _uaname=put(ua,$uauc.); if index(_uaname,'Urbaniz') then uatype='UA'; else uatype='UC'; *--urban cluster or urbanized area?--; end; format County County2 $county. PlaceName $fplace. CBSAName $cbsa. nectaname $necta. MetDivname $metrodiv. csaname $csa.; *** format PUMA12Name $puma. UAName $uauc.; 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=esriid state zcta5 zipname stab PctState SumLev IntPtLat Intptlon Landsqmi AreaSQMI TotPopSF1 TotPop MedianAge pctUnder18 pctOver65 pctwhite1 pctblack1 pctasian1 pctHispanicPop TotHHs MedianHHInc FamHHs MedianFamInc PovUniverse PctPoor pctGrpQuarters pctBachelorsormore pctInCollege pctForeignBorn TotHUs OccHUs pctRenterOcc MedianHValue MedianGrossRent Rename=(TotPopSF1=TotPop10) Where=(state="&state" and zcta5 ne '99999' and TotPop) ); if zcta5 ne ' '; ACSYears="&acsyears"; ** drop Years; *<=================Variable Years not on ACS set this cycle-; format AreaSQMI LandSQMI 9.2 ; rename TotPop=TotPopACS; label TotPop="Total Pop ACS &acsyears Period Est"; run; proc sort data= corrlst.zip_2_zcta15(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_zip2010_state(in=inzpst keep=zcta5 state nparts primary Pop2010 ZipTotPop pctpop rename=(nparts=Nstates primary=psf pctpop=pctState Pop2010=Pop10ZCTAstate)) user.zcta_splitstates(where=(state="&state") keep=state zcta5 state2 stab2 PctState PctState2 Pop10State2 in=insplit) altzips2(in=inaltzips) user.&stab.acsdata(in=inacs) ; *---Adding the splitstates set to merge for July 2014 run where we now have the secondary state data---; 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 (in1 or inacs) then delete; if not inacs then delete; *<====new criteria Aug. 2013===; label ZIPTotPop='ZCTA 2010 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; * if not in3 then pop2k=.; end; if not inaltzips then Naltzips=0; *** retain appsurl 'mcdc.missouri.edu/cgi-bin/applinks/applinks.py?geoid=86000USZZZZZ'; *substr(appsurl,48,2)="&stab"; ***substr(appsurl,62,5)=zcta5; format placename $place. cbsaname $cbsa. csaname $csa. ; *----Create the final value of ZIPName by using the name of the primary place, if applicable. We chop off the place type owrd ("city", "CDP", etc.) and replace it with ", &stab" ----; *if placefp ne '99999' then do; * length _pname $80 ; * _pname=put(placename,$place.); * %chopwrd(_pname); * if not (zipname=:_pname) then zipname=trim(_pname)|| ", &ustab"; *end; label Pop10ZCTAstate='2010 Pop in just the state portion of ZCTA'; drop _: ; run; %mend doit; options mprint; *<====; options compress=yes; %**dostates(38,names=1); %dostates(n=51,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(label='2012 vintage ZCTA Master for U.S.' compress=yes); set %dostates(n=51,mac=setref) open=defer; run; options nofmterr; data two; set user.zcta_master; where zcta5 in ('63105','97635'); *--Just pick two--; run; proc printto print='../zcta_master.sample2zips.txt' new; run; proc fsbrowse data=two printall; run;