x cd /pub/data/mable14/Tools; %let pgm=read_tgrlnfaces; filename pgm "&pgm..sas"; *---Read the TIGER/Line "faces" dbf files that related blocks to various geocodes. Coded and being run 7-15 to get new codes. The UA, PUMA12 (aka PUMA10) [unchanged] and revised ("current" in 2014 tgrn) place, cousub, cd(114), sldl, sldu, (various inc "best") school districts. Problem encountered with Bedford VA independent city (51115) going away and becoming part of Bedford county (51019). We had to change source of county codes referenced in the docos macro. John Blodgett, OSEDA, MCDC, ---*; *---Note that we have access to a directory of county-based "faces" data. They are inside .zip files. We used the system unzip command to extract the faces file and proc dbf to convert it to a SAS dataset. We then delete the extracted dbf file. ----; *---the data used here was downloaded via ftp from //ftp2.census.gov/geo/tiger/TIGER2014/FACES/ in July, 2015---; libname library (sasctlgs); *---the docos macro accesses the $county format code in library.formats-----; *-------------NOTE!!!! When this is done we will move the collection in tgrlnfaces to E:\backup\mcdc\data\tgrlnfaces14 on the mybook backup device stored in jgbs office.----------------------------------------------------------*; %let tgrpath=%str(/pub/data/mable14/tgrlnfaces); libname mable14 '/pub/data/mable14'; libname sf1 '/pub/data/sf12010' access=readonly; *---used by the docos macro to get county codes from sf1.uscounties-; libname user '/SAS_temp/user'; %let _resort=0; /*<=========================Comment out===================================== *---We also downloaded another zip file from the TIGER2014/CD directory on the FTP site. We stored a sample file in the mable14 directory and we shall now run code to look inside and see what we have on the dbf file inside.----; x cd /pub/data/mable14; %let cdfile=tl_2014_us_cd114; x unzip tl_2014_us_cd114.zip &cdfile..dbf ; *---Now we define a fileref pointing to the extracted dbf file--; filename dbfincd "&cdfile..dbf"; proc import datafile=dbfincd dbms=dbf replace out=mable14.uscd114 ; getdel=no; run; proc print data=mable14.uscd114(obs=25); title "mable14.uscd114"; *--Not very useful. Except maybe for names---; run; *-----convert a test county to see what we have this cycle------------; %let county=01001; x cd &tgrpath ; x unzip tl_2014_&county._faces.zip tl_2014_&county._faces.dbf ; *---Now we define a fileref pointing to the extracted dbf file--; filename dbfin "&tgrpath/tl_2014_&county._faces.dbf"; proc import datafile=dbfin dbms=dbf replace out=mable14.&pgm ; getdel=no; run; x rm tl_2014_&county._faces.dbf ; *---free up space. We still have the data inside the .zip file--; ***** data mable2k.&stab.tgrnlfaces(label="As downloaded and processed on &sysdate"); ===========================End commented out================================ */ %let st=01; %let stab=al; %global firstco; %let firstco=1; %*---Gets set to 0 within the doco macro after processing first county of a state-; x cd &tgrpath ; %macro doco(fipco,cname); %global firstco; x unzip -q tl_2014_&county._faces.zip tl_2014_&county._faces.dbf ; filename dbfin "&tgrpath/tl_2014_&county._faces.dbf"; proc import datafile=dbfin dbms=dbf replace out=work.&pgm ; getdel=no; run; x rm tl_2014_&county._faces.dbf ; *---free up space. We still have the data inside the .zip file--; filename dbfin clear; %let countychanged=0; *----Create a block to UA, TAZ and PUMA12 (or whatever is latest) equivalency file---------------*; data facecodes; set work.&pgm (keep=statefp10--blockce10 suffix1ce uace10 pumace10 statefp countyfp tractce blkgrpce cousubfp placefp elsdlea scsdlea unsdlea cd114fp sldust sldlst ); length state $2 County $5 cnty $3 Tract $7 Block $4 blksfx $1 UA $5 UR $1 PUMA12 $5; *---We shall save space by not keeping these --14 vars but we do need to put them in later because a few have changed--; **length state14 $2 County14 $5 cnty14 $3 tract14 $7 bg14 $1 ; **Decided not to keep these**; length cousubfp14 $5 placefp14 $5 SDElm14 SDSec14 SDUni14 SDbest14 $5 SDbesttype14 $1 cd114 $2 County14 $5 sldu14 sldl14 $3; *<---note adding county14------------; State=statefp10; cnty=countyfp10; County=state||cnty; Tract=substr(tractce10,1,4)||'.'||substr(tractce10,5,2); Block=blockce10; blksfx=suffix1ce; *We keep county14 since we have had an instance (independent city 51515 going back to be part of 51019) where county changed-; county14=statefp||countyfp; retain _countychanged 0; if county14 ne county then _countychanged+1; if _countychanged=1 then call symput('countychanged','1'); **State14=statefp; ** cnty14=countyfp; ** County14=state14||cnty14; **Tract14=substr(tractce,1,4)||'.'||substr(tractce,5,2); ** BG14=blkgrpce; cousubfp14=cousubfp; placefp14=placefp; SDElm14=elsdlea; SDSec14=scsdlea; SDUni14=unsdlea; *---bestsd is new for this cycle. There are 3 school dists but they are mutually exclusive in MO and other states. So we used this to store the relevant code. SDbesttype contains letters u, e or s to indicate type of district in SDbest-; if sduni14 ne ' ' then do; sdbest14=sduni14; sdbesttype14='u'; end; else if sdelm14 ne ' ' then do; sdbest14=sdelm14; sdbesttype14='e'; end; else if sdsec14 ne ' ' then do; sdbest14=sdsec14; sdbesttype14='s'; end; cd114=cd114fp; UA=uace10; PUMA12=pumace10; if UA=" " then UR="R"; else UR="U"; label UR="Urban/Rural"; sldu14=sldust; sldl14=sldlst; keep State--sldl14; run; %if &countychanged %then %do; %put *****One or more County Codes changed processing this county: &county *****; %let _resort=1; %end; %let lnfaceCodes=placefp14 cousubfp14 SDbest14 SDbesttype14 sldu14 sldl14; %let curBasecodes=State14 county14 tract14 bg14; proc sort; by county tract block blksfx ; run; *--and now sort by the target codes as well--; *---Extra step inseerted new for 2014. We look for duplicate blocks (signaled by blksfx values) and keep the version which occurs most frequently.---; data maxn_blksfxs; set facecodes; by county tract block blksfx; where blksfx ne ' '; if first.block then do; _na=0; _nb=0; _nc=0; _nd=0; _nother=0; end; if blksfx='A' then _na+1; else if blksfx='B' then _nb+1; else if blksfx='C' then _nc+1; else if blksfx='D' then _nd+1; else do; _nother+1; _savesfx=blksfx; end; if last.block then do; _max=_na; blksfxmax='A'; if _nb > _max then do; _max=_nb; blksfxmax='B'; end; if _nc > _max then do; _max=_nc; blksfxmax='C'; end; if _nd > _max then do; _max=_nd; blksfxmax='D'; end; if _nother > _max then blksfxmax=_savesfx; output; end; keep county tract block blksfxmax; run; data facecodes2(label="With lower count blksfxs deleted"); merge facecodes(in=in1) maxn_blksfxs(in=in2); by county tract block; if in2 then if blksfx=blksfxmax; *<---this is the filter saying we only keep data for one suffix code--; drop blksfxmax; run; *----and now eliminate dups after filtering out the non-max alternatives---; proc sort data=facecodes2 nodupkey out= %if &firstco %then %str(mable14.&stab.tgrlnfaces); %else c&fipco.faces; ; by county tract block; run; *---We have now created the dataset for this county. If it is the first county we write directly to the state set. For all counties after the 1st we write to a temporary c&fipco.faces set and then we use proc append to add it to the state dataset.---*; %if &firstco=0 %then %do; proc append base=mable14.&stab.tgrlnfaces data= c&fipco.faces; run; %end; %let firstco=0; %mend doco; options mprint; *----do a test county run here -------------------------------------*; %**let firstco=1; %**let county=01001; %**doco(01001,Autauga); options NOmprint; data uscom14; set popests.uscom14; keep state county areaname; format county $5.; run; %macro doit(state,stab); %let _resort=0; *---if county codes have changed we need to re-sort the final data set--; %let st=&state; %global firstco; %let firstco=1; %***docos(state=&st,mac=doco,codataset=sf1.uscounties); %docos(state=&st,mac=doco,codataset=user.uscom14); data _null_; set mable14.&stab.tgrlnfaces end=last; by county notsorted tract notsorted; retain _resort '0'; if first.county then do; if _n_ > 1 and county lt _curcounty then _resort='1'; _curcounty=county; retain _curcounty; _curtract=tract; _curblock=block; retain _curtract _curblock; end; else if first.tract then do; if tract gt _curtract then _resort='1'; _curtract=tract; _curblock=block; end; else if block gt _curblock then _resort='1'; _curblock=block; if last then call symput("_resort", _resort); run; %if &_resort %then %do; proc sort data=mable14.&stab.tgrlnfaces; by county tract block; run; %put ***mable14.&stab.tgrlnfaces has been resorted ****; %end; %mend doit; %dostates(01 02 04 05 06 08 09 11 12,msgs=1); %dostates(13 15 16 17 18 19 20 21 22 23 24 25 26 27 28,msgs=1); %dostates(29 30 31 32 33 34 35 36 37 38 39 40 41 42 44 45 46 47,msgs=1); %dostates(48 49 50,msgs=1) ; %**doit(51,va) ; %dostates(53 54 55 56,msgs=1) ; *---On July 1,2013 the indpependent city of Bedford, VA became a town and rejoined Bedford County. So 51515 is gone! We have to make sure we do not try to process a tigrnln file for that defunct county. Also means we have a 2010 and a 2014 county.---; %dostates(51,msgs=1) ; *--Running 9-8-15 with sorting of faces file added---; options NOmprint; /*<=========================Comment out===================================== *----Interactive steps to look at cases where we had dup keys for our test county (10001)--; proc sort data=user.facecodes; by county tract block &lnfaceCodes; run; data dupblks3; length ngeos 8; format ngeos 3.; set user.facecodes; by county tract block &lnfacecodes; keep county tract block ngeos; if first.block then ngeos=0; if last.sldl14 then ngeos+1; if last.block and ngeos>1 then output; run; data dupblks4; merge user.facecodes dupblks3(in=indups); by county tract block; if indups; run; proc print data=dupblks4; by county tract block blksfx notsorted; id county tract block blksfx; var &lnfaceCodes ngeos; run; ===========================End commented out================================ */