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. Doing a major update 9-14-15 regarding how we choose the best line face set of geocodes for a block. We tested some some hypotheses in temptgrlnfaces51xxx.sas and will now incorporate what we learned here. Previous version saved in read_tgrlnfaces_notcurrent.sas . 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 https://www2.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; data %if &firstco %then %str(&stab.tgrlnfaces); %else %str(c&fipco.faces); ; set work.&pgm (keep=statefp10--blockce10 suffix1ce uace10 pumace10 statefp countyfp tractce blkgrpce cousubfp placefp elsdlea scsdlea unsdlea cd114fp sldust sldlst Atotal); %let _state=%substr(&fipco,1,2); where statefp10="&_state"; *<---added to prevent missing blocks 10-9-15---; 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'); if (county14 ne county) and (county ne '51115') then do; county14=county; end; **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 Atotal; run; %let lnfaceCodes=placefp14 cousubfp14 SDbest14 SDbesttype14 sldu14 sldl14; %let curBasecodes=State14 county14 tract14 bg14; *---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=&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); data _null_; file stdout; put "mable14.&stab.tgrlnfaces is being processed."; run; %let st=&state; %global firstco; %let firstco=1; %***docos(state=&st,mac=doco,codataset=sf1.uscounties); %docos(state=&st,mac=doco,codataset=user.uscom14); proc sort data=&stab.tgrlnfaces ; by county tract block blksfx; run; data user.facecodes; set &stab.tgrlnfaces; by county tract block blksfx; if first.blksfx then do; nfaces=1; SumAtotal=Atotal; end; else do; nfaces+1; SumAtotal + Atotal; end; if last.blksfx; *---We only allow county14 to differ from county for the change in Bedford City VA which changed to Bedord county instead of being an independent city contained (geographically) within Bedford county.---; if county="51515" then county14="51019"; drop atotal; run; proc sort; by county tract block descending SumAtotal; run; run; data mable14.&stab.tgrlnfaces; set user.facecodes; by county tract block; if first.block; *---because we sorted descending the first obs has the greatest value--; drop nfaces sumAtotal; run; data uhoh; set user.facecodes end=last; by county tract block blksfx notsorted; if first.blksfx then do; sdbest14_=sdbest14; sdbesttype14_=sdbesttype14; sldu14_=sldu14; sldl14_=sldl14; cousubfp14_=cousubfp14; placefp14_=placefp14; retain sdbest14_ -- placefp14_; end; else if not ( (sdbest14_=sdbest14) & (sdbesttype14=sdbesttype14_) & (sldu14_=sldu14) & (sldl14_=sldl14) & (cousubfp14_=cousubfp14) & (placefp14_=placefp14 )) then do; output; _nproblems+1; if _nproblems > 20 then do; file stdout; put _nproblems " problems (stops after 21) detected for &stab"; stop; end; end; if last and _nproblems then do; file stdout; put _nproblems " inconsistent geos problems (stops after 21) detected for &stab"; end; run; data _null_; file stdout; put "mable14.&stab.tgrlnfaces has been created"; run; %mend doit; /*<=========================Comment out===================================== %*----------Do full run 9-23-15--------------------; %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) ; %**dostates(53 54 55 56,msgs=1) ; %dostates(55 56,msgs=1) ; %**doit(51,va) ; *---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---; ===========================End commented out================================ */ %dostates(n=51,msgs=1); options NOmprint;