x cd /pub/data/acs2015/Tools; %let pgm=usbgprofiles; filename pgm "&pgm..sas"; /*--Generate acs.usbgs5yr J. Blodgett, OSEDA, Dec, 2015 . Input data set acs.usbgstemp was created by mcdcprofiles5yr.sas. The acs.newbgvars5yr was created by job newbgvars.sas . Final run involves using a newbgvars update set. Modifying and rerunning 5-4-16 to incorporate better matching to SF1 in cases where the BG codes have changed for ACS. Running with 2015 vintage data 12-8-16. Running again 12-13-16 to add the newbgvars data into the usbgs5yr dataset. -- */ title "&pgm"; options compress=yes; %let vintage=2015; %let period=5yr; *<=====; %let year=&vintage; libname acs2015 '/pub/data/acs2015'; *<=====; libname acs (acs&vintage); libname user '/SAS_temp/user'; filename nomatch "&pgm..nomatch.txt"; *---here we write messages indicating geos not matched between acs and sf1---; %global nvarsnd; %ndefvars(acs.usbgstemp,charvars=0) ; libname sf12010x '/pub/data/sf12010x'; libname sf1x (sf12010x); libname mable14 '/pub/data/mable14' access=readonly; /*<=========================Comment out===================================== *----We broke the original usmcdcprofiles5yr dataset into 3 pieces: usbgstemp, ustractstemp and the rest (usmcdcprofiles5yralt) .-----; data usbgsacsTemp(label='Temp blkgrp level data with limited variables' compress=no); retain vintage "&vintage" period "&period"; *<===========================; length esriid $12 county $5 tract $7 bg $1; *<====note: we renamed puma5 to puma prior to running this step ===; ** drop=areaname period years geocomp_name geocomp placefp geocomp_name UA--CongDist TAZ concit--aianhh FullTimeWorkers--pctPoorOver65 FullTimeWorkers_moe--poorOver65_moe Over16--pctlaborforce military pctmilitary pctEmployedFemales Over16_moe--EmployedFemales_moe Women15to50--pctGrandPrntsCaring5orMore Women15to50_moe--GrandPrntsCaring5orMore_moe TotPopNonInst--pctDisabledElder TotPopNonInst_moe--DisabledElder_moe Over1--pctSpanishEnglishLTD Over1_moe--SpanishEnglishLTD_moe ; set acs.usbgstemp( drop=areaname period years geocomp_name geocomp placefp geocomp_name UA--CongDist TAZ concit--aianhh &nvarsnd ); ***the nvarsnd global variable is defined above using the ndefvars macro ****; *** where sumlev='150'; *--already a subset--; **esriid=substr(geoid,8); county=substr(geoid,8,5); tract=substr(geoid,13,4)||'.'||substr(geoid,17,2); BG=substr(geoid,19,1); **period='5yr'; drop geoid; label sumlev=' '; run; ===========================End commented out================================ */ /*<=========================Comment out===================================== *----Create alternate bg level sf1x sets for AK and SD where we had county changes in 2015--; data sf1x.akbgs150B(label="With updated county codes for Wade Hampton") sf1x.sdbgs150B(label="With updated county codes for Shannon"); set sf1x.akbgs150 sf1x.sdbgs150; if county='02270' then county='02158'; if county='46113' then county='46102'; substr(esriid,1,5)=county; if state='02' then output sf1x.akbgs150B; else output sf1x.sdbgs150B; run; proc sort data=sf1x.akbgs150B; by esriid;; run; proc sort data=sf1x.sdbgs150B; by esriid;; run; *---we have 51 state-level sf1x datasets. Here we concatentate and shrink (selects vars) those sets.--; %macro setref(state,stab); %if &state=02 or &state=46 %then %let Bsfx=B; %else %let Bsfx=%str(); sf12010x.&stab.bgs150&Bsfx (keep=esriid areaname state stab county tract bg intptlat intptlon LandSQMI AreaSQMI totpop totpop00 tothhs GQpop rename=(totpop=totpopsf1 GQpop=Gqpopsf1 tothhs=TotHHssf1)) %mend setref; data sf1xusbgs(compress=no); set %dostates(n=51,mac=setref) open=defer; run; *-----We are also going to apply special updates to some of the geocodes so that we can match the acs geocodes that have changed and would otherwise not match the sf1 data. jgb, 5-4-16. Modeled after code used in the mcdcprofiles5yr setup that worked with the ustracts5yr dataset.----; data bedford_city_bgs; set sf1x.vabgs150 (keep=esriid sumlev county tract bg intptlat intptlon LandSQMI AreaSQMI totpop tothhs rename=(totpop=TotPopSF1 tothhs=TotHHsSF1)); where county='51515'; county='51019'; put _all_; put county= $5.; run; *--the SAS update stmt requires a single updata data set. So we combine our 2--; data bgupdates(compress=no); length esriid $12; set mable14.alt_sf1data(in=inaltdata rename=(tractACS=tract bgACS=bg pop10=TotPopSF1 hus10=TotHHsSF1) where=(sumlev='150') ) bedford_city_bgs sf1xusbgs(in=onsf1); by county tract bg; if first.bg; *--when a bg was on SF1 but then changed defn it is also on mable14.alt_sf1data and thats the one we keep--; esriid=county||compress(tract,'.')||bg; if onsf1 then newcode='n'; else newcode='y'; *--flag bfgs that have a new code in the AC--; run; data _null_; file nomtch; put; run; *---clear out file for do-over runs---; data usbgs5yrtemp( label="National 5-yr Summaries for BGs with SF1 data" ); update bgupdates(in=insf1) usbgsacstemp(in=inacs where=(state ne '72')) end=_lastobs; by esriid; *drop geocode; retain _nomatch 0; retain cbsayr '2015'; *--Even though it comes from sf1 it is still current as of 12/12 ---; label totpopsf1='2010 Census Total Pop count' tothhssf1='Total HHs 2010 Census count'; if not (inacs and insf1) then do; file nomatch mod; if _nomatch=0 then put / "Unmatched tempusbgs observations. As run at &sysdate &systime"; _nomatch+1; if _n_ gt 100 and _n_ - _nomatch < 20 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 esriid= county= $county.; if not inacs then delete; end; if _lastobs and _nomatch then do; file log; put '**** ' _nomatch ' observations not matched between acs and sf1 '; end; drop _: ; label sumlev=' '; run; data acs.usbgs5yr( index=(state) label="National BG data with SF1 data merged" ); set usbgs5yrtemp; run ; *<====for now we just copy it===============; ===========================End commented out================================ */ *---12-13-16 . After earlier running the copy step without additional vars (because the bg summary tables were not ready) we now go back and create the enhanced version of usbgs5yr with the added variables from newbgvars.; *----------------This update step gets us a few more variables that before were all missing-------------------*; data acs.usbgs5yrB( index=(state) label="National BG data with SF1 data merged" ); update usbgs5yrtemp (in=inmaster) acs.newbgvars5yr(keep=esriid state logrecnoC _numeric_ where=(state ne '72') in=innewvars) end=_lastobs; by esriid; if inmaster; if innewvars then do; array nums(*) ManProfOccs ServiceOccs SalesOffOccs FarmFishOccs ConsOccs TransOccs Agriculture Construction Manufacturing WholesaleTrade RetailTrade Transportation Information Finance_ins Professional Educ_health_SocSvcs RecreationEtc OtherIndustries PublicAdmin PrivWageWorkers GovWorkers SelfEmpWorkers UnpaidFamWorkers Civilian ArmedForces; array pcts(*) PctManProfOccs PctServiceOccs PctSalesOffOccs PctFarmFishOccs PctConsOccs PctTransOccs PctAgriculture PctConstruction PctManufacturing PctWholesaleTrade PctRetailTrade PctTransportation PctInformation PctFinance_ins PctProfessional PctEduc_health_SocSvcs PctRecreationEtc PctOtherIndustries PctPublicAdmin PctPrivWageWorkers PctGovWorkers PctSelfEmpWorkers PctUnpaidFamWorkers PctCivilian PctArmedForces; if EmployedCLF then do _i=1 to dim(pcts); pcts{_i}=nums{_i}*100/EmployedCLF; end; end; if TotPopSF1=. then put '**no sf1 data: ' county= $county. county $5. +1 tract= bg=; format landsqmi 9.2; drop metrodiv; drop _i; run; *---If it ran successfully we now do the name changes. -------------------------------------------------; proc datasets nolist library=acs&year; change usbgs5yr=old_usbgs5yr; change usbgs5yrB=usbgs5yr; modify usbgs5yr; index create county; quit; data two; set acs.usbgs5yr(obs=2); run; proc fsbrowse printall data=two; title2 'acs.usbgs5yr'; run;