x cd /home/riceg/ACS_SAS_processing; %let pgm=cnvt_geo_files5; filename pgm "&pgm..sas"; /* Read data from the 5-year geo-header summary files ascii files. */ %let year=2017; %let yearm4=%eval(&year-4); %let yr=%substr(&year,3,2); %include '/pub/sas/autoexec.sas'; title "&pgm.: Convert &year 5-year Geographic headers Data"; options compress=yes; libname acs&year "/pub/data/acs&year"; libname user '/home/riceg/ACS_SAS_processing/temp'; %let geosetname=allgeos2013_2017; libname georef '/pub/data/georef' access=readonly; *-- Read and store the geographic identifiers CSV file ; filename geos "/pub/data/acs&year./base_tables_5yr/geocodes5yr.csv"; %macro lzeros(var,len); if &var ne " " and length(&var) < &len then do; &var=right(&var); &var=translate(&var,'0',' '); end; %mend lzeros; data user.allgeos5; infile geos lrecl=500 dsd delimiter="," truncover firstobs=1; length SumLev $3 geoid $40 AreaName $100 Geocomp_Name $2 esriid $16 State Stab $2 Geocomp $2 LogRecno 8 FipCo County $5 CousubFP PlaceFP $5 Tract $7 bg $1 CBSA $5 cbsaType $5 metdiv $5 CSA $3 NECTA $5 UA $5 PUMA $5 sdelm sdsec sduni $5 Region Division $1 CongDist $2 TAZ $3 ; length set $3; *<-- This variable is our invention. Values are bgs, trs, and all. How we plan to split our datasets, with block group and tract levels in separate sets. ; cbsa=' '; cbsaType=' '; CBSAYr=' '; metdiv=' '; *-- being retained because we did a keyed read below. ; length _acssf $5 _cnty $3 _tractin $6 concit $5 aianhhfp $5aihhtli $1 aits $5 anrc $5 macc memi $1 cnecta $3 nectadiv $5 sldl sldu $3 zcta5 $5 submcd $5 ur $1 pci $1 _geoid $40 _Name $1000 ; input _acssf stab sumlev geocomp logrecno _US $ region division _statece $ state _cnty cousubfp placefp _tractin bg concit aianhh $ aianhhfp aihhtli _aitsce $ aits anrc cbsa csa metdiv macc memi necta cnecta nectadiv ua _uacp $ congdist sldl sldu _vtd $ _zcta3 $ zcta5 submcd sdelm sdsec sduni ur pci taz _uga $ puma _puma1 $ _geoid _name ; if _acssf ne 'ACSSF' then do; file log; put "Input data line does not contain correct filetype in cols 1-5 (ACSSF expected)"; list; abort abend; end; if _n_=1 then list; *-- Make sure we are getting that first record for Alaska no geocomp. ; *-- Prior to the 2016 files, these geocodes appeared as character string with leading zeros. But not this time! ; %lzeros(Geocomp,2); if _n_=1 then if geocomp ne '00' then do; put _all_; stop; end; %lzeros(Sumlev,3); %lzeros(_stateCE,2); %lzeros(State,2); %lzeros(_cnty,3); %lzeros(cousubfp,5); %lzeros(placefp,5); %lzeros(tractin,6); %lzeros(cbsa,5); %lzeros(csa,3); %lzeros(necta,5); %lzeros(ua,5); %lzeros(uacp,5); %lzeros(puma5,5); %lzeros(puma1,5); %lzeros(sdelm,5); %lzeros(sdsec,5); %lzeros(sduni,5); %lzeros(congdist,2); %lzeros(sldu,3); %lzeros(sldl,3); %lzeros(zcta5,5); %lzeros(summcd,5); _ix=index(areaname,'0D'x); *-- These characters somehow were appended to the areanames. ; if _ix then substr(areaname,_ix)=' '; if sumlev='860' then do; *-- add the "primary" state codes for ZCTAs ; stab=put(zcta5,$zipstab2.); state=put(stab,$stabfip2.); if zcta5 < '01000' then do; stab='PR'; state='72'; end; if '20200' <= zcta5 <= '20299' then do; stab='DC'; state='11'; end; end; if state='00' then state=' '; if stab=' ' and state=' ' then do; _ndelete+1; if _ndelete le 25 then put sumlev= areaname= _geoid= ' **deleted**'; if _ndelete ge 25 then stop; if sumlev='860' then put / _all_; end; geoid=_geoid; esriid=substr(geoid,8); *-- the codes identifying the area without SumLev and Geocomp and "US" from geoid ; if _cnty ne ' ' then County=State||_cnty; else County=' '; Fipco=county; format fipco $5. county $county.; if geocomp ne '00' then do; _idashdash=index(_name,'--'); Areaname=substr(_name,1,_idashdash-1); Geocomp_Name=geocomp; format geocomp_name $geocomp.; *-- so the code displays as the geocomp title ; end; else Areaname=_name; if county ne ' ' then do; if county ne lag(county) and state ne '72' then do; *-- get the metro codes attached to any level with county specified. ; _savecounty=county; if county in ('02158','46102') then do; /* These are 2 county changes in Alaska (Wade Hampton to Kusulvak) and SD (Shannon to Oglala Lakota) were not picked up for the georef.cbsacos dataset. So we have to temporarily change county back to do keyed read. */ if county='02158' then county='02270'; else if county='46102' then county='46113'; end; set georef.cbsacos(keep=county cbsa cbsatype metrodiv csa Year rename=(year=CBSAyr metrodiv=metdiv)) key=county; county=_savecounty; _cbsa=cbsa; _cbsatype=cbsatype; _metdiv=metdiv; _csa=csa; _cbsayr=cbsayr; retain _cbsa--_cbsayr; end; else do; cbsa=_cbsa; cbsatype=_cbsatype; metdiv=_metdiv; csa=_csa; cbsayr=_cbsayr; end; end; else if cbsa ne ' ' then do; *-- County is blank. Presumably cbsa or partial cbsa summaries ; if index(areaname,'Micropolitan') then cbsatype='micro'; else cbsatype='metro'; cbsayr=_cbsayr; end; else if county=' ' then do; cbsa=' '; cbsatype=' '; metdiv=' '; csa=' '; necta=' '; end; if sumlev in ('080','140','150') then do; *-- Actually 080 is not on the 2013 file we are told ; if length(_tractin)=4 then substr(_tractin,5,2)='00'; Tract=substr(_tractin,1,4)||'.'||substr(_tractin,5,2); end; /* This is a problem caused by the data for PR coming in with the 310 levels (etc) having PR as the stab. These should have been with the us summaries (stab='US'). But if we change PR to US then we have a problem because logrecno will not be sequential within the US group. Kind of a mess. */ if stab='PR' and state=' ' then stab='US'; label CongDist='115th Congressional District'; *<================; drop _: ; if sumlev ne lag(sumlev) then do; if sumlev='150' then set='bgs'; else if sumlev in ('080','140') then set='trs'; else set='all'; retain set; file log; end; label geoid='Geographic ID (Census)' SumLev='Geog summary level' Geocomp='Geographic component' PlaceFP='Place (city) code' CousubFP='County subdivision (town) code' metdiv='Metro Division of MSA' CSA='Consolidated Stat Area' NECTA='New England City/Town Area' UA='Urban Area/Cluster' PUMA='2010 PUMA ' sduni='Unified School District' sldu='State Legislative District - Upper Chamber' sldl='State Legislative District - Lower Chamber' ZCTA5='5-digit ZIP/ZCTA' ; *label bttr='Tribal Tract' BTBG='Tribal BG'; *-- gone this cycle? ; run; proc sort out=acs&year..&geosetname (index=(state) label="Geography for ACS 5yr data vintage &year"); by State geoid; run; options ls=160; proc freq data=acs&year..&geosetname ; table sumlev*set/nopercent nocum; format sumlev $sumlevwc.; table state*stab/list missing ; table geocomp*geocomp_name/list; format geocomp_name $geocomp40.; run; %include sascode(notify);