x cd /home/riceg/ACS_SAS_processing; %let pgm=mcdcprofiles5yr; filename pgm "&pgm..sas"; /* Running after process_mcdcprofiles5yr.sas which does the basic convert to create the acs.usmcdcprofiles5yr data set. Here we create multiple data sets based on geography. The partitioning is modeled after the way we processed the sf1 national file for 2010. In fact, we attempt to match obs on the parallel data sets and transfer the 2010 census total pop figure and some GIS vars (lat-long coords, areas) as part of the output acs data sets. Added code to proc datasets step at bottom to have state and county level indices created for ustracts5yr dataset. 11-30-16. Note that we do not have sf1 data for metros or for county portions of places and cdslds. In the latter case, we went ahead and did the merge but the sf1-based variables are missing for the within-county summaries. NOTE! We shall do the bgs datasets in a separate run, usbgprofiles.sas . */ %include '/pub/sas/autoexec.sas'; %let year=2018; %let yr=%substr(&year,3,2); %let yearm4=%eval(&year -4); %let yrm4=%substr(&yearm4,3,2); %let fixpuma5=0; *-- if variable puma got named puma5 instead we "fix" it with a rename per this global parm ; libname acs "/pub/data/acs&year"; libname sf1x '/pub/data/sf12010x'; libname mable14 '/pub/data/mable14' access=readonly; *--access the alt_sf1data to get sf1 data for tracts changed for ACS-; filename nomatch "&pgm..nomatch.txt"; *-- here we write messages indicating geos not matched between acs and sf1 ; libname user '/home/riceg/ACS_SAS_processing/temp'; options compress=binary; /*<=========================Comment out===================================== ===========================End commented out================================ */ *-- let's begin by regenerating the input dataset so that all the _moe variables are at the end ; data allxmoe; set acs.usmcdcprofiles5yr(drop=%varlist(*_moe,acs.usmcdcprofiles5yr) ); run; data moeonly; set acs.usmcdcprofiles5yr(keep=%varlist(*_moe,acs.usmcdcprofiles5yr) ); run; data acs.usmcdcprofiles5yralt(label='All sumlevs exc tract and bg, _moe vars last' drop=tract bg index=(sumlev)) acs.ustractstemp acs.usbgstemp; ; merge allxmoe moeonly; if sumlev='140' then output acs.ustractstemp; else if sumlev='150' then output acs.usbgstemp; else output acs.usmcdcprofiles5yralt; %macro fixit; %if &fixpuma5 %then %do; rename puma5=puma; %end; %mend fixit; %fixit ; run; proc datasets library=acs nolist; modify usmcdcprofiles5yralt; index create state; quit; *-- Begin Part II: Breaking data down into geography-based subsets ; %let dgc=%str(drop=geocomp geocomp_name); data acs.usgeocomps5yr(label="5-yr Summaries for geographic components of various geographies") acs.usregdiv5yr(label="5-yr Summaries for the nation, Regions, and Divisions" compress=no &dgc) usstcnty5yr(label="National 5-yr State and County Summaries" &dgc) uscousubs5yr(label="National 5-yr County Subdivision/MCD Summaries" &dgc) usplaces5yr(label="Place (city &dgc) and place/county summaries" &dgc) uscdslds5yr(label="National 5-yr CD115 and 2018 State Leg District summaries" &dgc) uszctas5yr(label="National 5-yr ZIP Census Tab Area summaries" &dgc index=(state)) acs.usamindianetc5yr(label="National 5-yr Indian Reservation etc summaries" &dgc) acs.usmetros5yr(label="National 5-yr summaries for MSAs (CBSAs), CSAs, Metro Divs, etc, 2013 defs" &dgc) acs.uspumas5yr (label="National 5-yr summaries for 2010 PUMAs" &dgc) usschools5yr(label="National 5-yr summaries for School Districts" &dgc) acs.usurbareas5yr(label="National 5-yr summaries for urban areas as defined in 2002" &dgc) ustractsH5yr(label="National 5-yr summaries for hierarchal tracts (level 080)" &dgc) ustracts5yr(label="National 5-yr summaries for complete Census tracts " &dgc) ; length SumLev $3 esriid $22 vintage $4 period $3; *-- increased esriid length to handle 080 levels which are 21 long ; set acs.usmcdcprofiles5yralt acs.ustractstemp; by geoid; *-- forces the sort order ; retain _first 1; retain vintage "&year"; drop years; format geoid areaname; format geocomp_name $geocomp40. ; label geoid=' ' areaname=' ' state=' ' geocomp=' ' county=' ' puma=' ' cbsa=' ' placefp=' ' metdiv='Metro Div.' DateCreated=' '; if period='5' then period='5yr'; if _first then do; *-- Value of years was 2013 on input set for 2013. So we let it go by adding or condition. ; if period='5yr' and (years=:"&yearm4" or years="&year") then ; else do; file log; put "******Problem with input dataset. We need the 5yr data file for vintage &year but we find: " period= vintage= years= / '***Conversion will abort***'; abort abend; end; _first=0; end; * format IntPtLon 11.6 IntPtLat 10.6; if sumlev='150' then delete; *-- Do the block group data separately. See setup usbgprofiles.sas, at least for now. ; if sumlev in ('067','070','170','172','258','259','293','294','354','312','313','316','352','353','354','357','358','550') then delete; if sumlev in ('320','321','322','323','324','332','333','337','338','340','341','345','346','355','356', '360','361','362','363','364','365','366' ) then delete; *-- redundant or unwanted metro levels ; if state='72' then delete; *-- avoid PR and all its special cases ; esriid=substr(geoid,8); *-- As of 3-3-12 there are lots of missing ID variables on the incoming usmcdcprofiles5yr dataset. While we plan to fix this we are not assuming that many of these variables are available here. Instead we plan to rely on the merging with the corresponding sf1x datasets and picking up the other key ID variables from those sets. We shall be matching on esriid. ; *-- output the data sets. *; *-- On first run we had usregdistates and counties. But decided to group states with counties for final run, now usregdivs and usstcnty ; if geocomp gt '00' then output acs.usgeocomps5yr; else if sumlev le '030' then output acs.usregdiv5yr; else if sumlev='050' or sumlev='040' then output usstcnty5yr; else if sumlev='060' then output uscousubs5yr; else if sumlev ='080' then output ustractsH5yr; else if sumlev ='140' then output ustracts5yr; else if sumlev='860' then output uszctas5yr; else if sumlev in('160','155') then output usplaces5yr; else if sumlev in ('500','610','620') then output uscdslds5yr; else if ('250' <= sumlev <= '299') or sumlev='230' then output acs.usamindianEtc5yr; else if ('310' <= sumlev <= '370') then output acs.usmetros5yr; else if sumlev in ('950','960','970') then output usschools5yr; else if sumlev='795' then output acs.uspumas5yr; else if sumlev in ('400','410','430') then output acs.usurbareas5yr; else put '**** sumlev not expected: ' sumlev=; drop _: ; run; *-- Alternate step 2 where all we process this time is the zcta data ; %macro altstep2( ); data uszctas5yr(label="National 5-yr ZIP Census Tab Area summaries" &dgc index=(state)); length SumLev $3 esriid $5; set acs.usmcdcprofiles5yralt; where sumlev='860'; by geoid; *-- forces the sort order ; retain _first 1; format geoid areaname geocomp_name ; label geoid=' ' areaname=' ' state=' ' geocomp=' ' county=' ' puma=' ' cbsa=' ' placefp=' ' metrodiv='Metro Div.' DateCreated=' '; if state='72' then delete; esriid=substr(geoid,8); drop _: ; run ; *-- We are already creating the set in the big step, just above- ; %mend altstep2; %*altstep2 ; data _null_; file nomatch; put; run ; *-- clear out nomatch file so we can write to it mod; ods listing close; %let totpop00=totpop00; *-- Here we merge these ACS data sets with the corresponding 2010 sf1 extract sets where we hope to pick up the ID variables, lat-long coordinates and 2000 populations. ; %macro mergem(setname,setname1x=,dropvars=,index=1,sort=0,byvars=esriid,geos=,morenumvars=); %if &sort %then %do; proc sort data=&setname force; by &byvars; run; %end; %if &setname1x=%str() %then %let setname1x=&setname; %let _lennamem2=%eval( %length(&setname1x) - 2); %let _lennamem3=%eval( %length(&setname1x) - 3); %if %substr(&setname1x,&_lennamem2,3)=%str(5yr) %then %let setname1x=%substr(&setname1x,1,&_lennamem3); %put ***setname=&setname setname1x=&setname1x _lennamem2=&_lennamem2 _lennamem3=&_lennamem3 ; data acs.&setname ( %if &index %then %str ( index=(state)); label="National 5-yr Summaries for &geos with SF1 data" ); ; update sf1x.&setname1x (keep=_character_ intptlat intptlon LandSQMI AreaSQMI totpop &totpop00 &morenumvars rename=(totpop=TotPopsf1) in=insf1) &setname (in=inacs where=(state ne '72')) end=_lastobs; by &byvars ; drop geocode; retain _nomatch 0; label totpopsf1='2010 Census Total Pop count'; if sumlev='155' then insf1=1; *-- just to avoid the errors, since we know we do not have 155 levels on sf1.usplaces ; if sumlev='080' then insf1=1; *-- same thing ; if not (inacs and insf1) then do; file nomatch mod; if _nomatch=0 then put / "Unmatched &setname observations. As run at &sysdate &systime"; _nomatch+1; if _n_ gt 100 and (5*_nomatch > _n_) then do; file log; put "***Too many non matches. Step aborting ****"; stop; end; if inacs then _msg="Not in SF1"; else _msg="Not in ACS"; put _msg +1 sumlev= esriid= areaname= TotPop= TotPopsf1= ; if not inacs then delete; end; if _lastobs and _nomatch then do; file log; put '**** ' _nomatch ' observations not matched between acs and sf1 '; end; format landsqmi 9.2; drop _: &dropvars; ** tract cousubfp placefp ua zcta5 puma; label sumlev=' '; run; %mend mergem; options mprint; /*<=========================Comment out===================================== *-- We need to create new alternate versions of data for counties and county subs because of county name changes ; data sf1x.usstcntyB(label="With updated county codes"); set sf1x.usstcnty; if county='02270' then county='02158'; if county='46113' then county='46102'; if sumlev='050' then esriid=county; run; proc sort; by state county; run; data sf1x.uscousubsB(label="With updated county codes"); set sf1x.uscousubs; if county='02270' then county='02158'; if county='46113' then county='46102'; substr(esriid,1,5)=county; run; proc sort; by state county; run; ===========================End commented out================================ */ %mergem(usstcnty5yr,setname1x=usstcntyB,dropvars=tract cousubfp placefp ua zcta5 puma,sort=1,geos=States and Counties) ; %mergem(uscousubs5yr,setname1x=uscousubsB,dropvars=tract placefp ua zcta5 puma,geos=County Subdivisions) ; %mergem(usschools5yr,dropvars=cousubfp placefp tract ua zcta5 puma,sort=1,byvars=state sumlev esriid,index=1,geos=School Districts) ; data sf1x.uscdslds2013temp; set sf1x.uscdsldspumas2012; where sumlev in ('500','610','620'); run; *-- all we have on the acs set ; %mergem(uscdslds5yr,dropvars=cousubfp placefp puma12 ua zcta5 puma,sort=1,byvars=state sumlev esriid, setname1x=uscdslds2013temp,index=1,geos=%str(114th CDs and 2016 SLDs) ) ; %mergem(usplaces5yr,dropvars=cousubfp tract ua zcta5 puma,sort=1,byvars=sumlev esriid) ; %let totpop00=; *proc datasets library=sf1x nolist ; *modify usmetros; *index create sumesr=(sumlev esriid); *quit; %*mergem(usmetros5yr,dropvars=,sort=1,geos=Metro Areas,byvars=sumlev esriid); %mergem(ustracts5yr,dropvars=cousubfp placefp ua zcta5 puma,geos=Census Tracts,byvars=sumlev esriid) ; %mergem(uszctas5yr,setname1x=uszips860,sort=0,byvars=zcta5,geos=ZCTAs,morenumvars=PctState PctCnty Naltzips); * Here we update the tracts5yr data set using the mable14.alt_sf1data set. It has data for tracts that have changed IDs since 2010. Grab the sf1x obs for the single tract in county 51515 Bedford city, VA. In ACS the county changes to 51019 (Bedford county). This is also where we grab the data for the counties in AK and SD that changed codes and names in 2016. ; data bedford_city_tract; set sf1x.ustracts (keep=sumlev county tract intptlat intptlon LandSQMI AreaSQMI totpop rename=(totpop=TotPopSF1)); where county='51515'; county='51019'; put _all_; put county= $5.; run; data shannon_wadehampton; *-- two renamed counties starting 2016 ; set sf1x.ustracts (keep=sumlev county tract intptlat intptlon LandSQMI AreaSQMI totpop rename=(totpop=TotPopSF1)); where county in ('02270','46113'); if county='02270' then county='02158'; else if county='46113' then county='46102'; put _all_; put county= $5.; run; proc sort data=shannon_wadehampton; by county tract; run; *-- not needed but what the heck ; *-- the SAS update stmt requires a single updata data set. So we combine our 3 ; data updates(compress=no); set mable14.alt_sf1data(in=inaltdata rename=(tractACS=tract pop10=TotPopSF1) where=(sumlev='140') drop=hus10 bgACS ) bedford_city_tract shannon_wadehampton; by county tract; run; data acs.ustracts5yrB; *-- We shall be renaming this data set and deleting the old version once we know it works ; update acs.ustracts5yr(in=inacs) updates; *by sumlev esriid; by county tract; if not inacs then do; _nbad+1; if _nbad le 10 then put "**Not matched on acs: " county= tract=; drop _nbad; delete; end; if TotPopSF1=. then put '***No SF1 pop data for ' state= county= $county. +1 tract= ; run; /*<=========================Comment out===================================== *<======= DO NOT RERUN this step without renaming data sets to undo what we do in proc datasets below ; *-- If it ran successfully, we now do the name changes. ; proc datasets nolist library=acs; change ustracts5yr=old_ustracts5yr; change ustracts5yrB=ustracts5yr; modify ustracts5yr; index create state; index create county; quit; ods html file="./&pgm..html"; title "&pgm run for Vintage &year Run on &sysdate"; %contdir(acs); run; ods html close; ===========================End commented out================================ */ %include sascode(notify);