x cd /pub/data/mable14/Tools; %let pgm=temptgrlnfaces51xxx; filename pgm "&pgm..sas"; *---Read the TIGER/Line "faces" dbf files that related blocks to various geocodes. Looking at detail for 2 VA counties . We used this temp/test setup to test some hypotheses and come up with a better way to process the tgrlnfaces data wrt choosing the best set of geocodes to be associated with each block. What we tried here and will incorporate into the read_tgrlnfaces.sas setup involves looking at the sum of Atotal by block-blksfx for all the counties (cannot just pick the best one for current county file because county does not always match the file. I.e. processing the file for 51015 does not mean that county=51015 within that file. And not all the references to 2010 blocks for county 51015 occur in that file. John Blodgett, OSEDA, MCDC, ---*; libname library (sasctlgs); *---the docos macro accesses the $county format code in library.formats-----; %let tgrpath=%str(/pub/data/mable14/tgrlnfaces); libname mable14 '/pub/data/mable14'; libname user '/SAS_temp/userb'; %let _resort=0; %let st=29; %let stab=mo; x cd &tgrpath ; %*let county=29019; data facecodesVA; set facecodes; length fileco $5; stop; run; *--Create base to allow proc appending--; %macro doco(fipco,cname); %global firstco; %***let fipco=29019; %***let cname=Boone; %let county=&fipco; 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=user.&pgm&county; 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; data facecodes; set user.&pgm&county (keep=statefp10--blockce10 suffix1ce uace10 pumace10 statefp countyfp tractce blkgrpce cousubfp placefp elsdlea scsdlea unsdlea cd114fp sldust sldlst atotal); retain fileco "&fipco"; 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 atotal fileco; run; %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--; proc append base=user.facecodesVA data=facecodes; run; /*<=========================Comment out===================================== ***Comment out this strategy. May not be best criteria plus a block can appear on more than 1 county file so we have to look at data for the entire state before we can choose the "best" obs for a block.; 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=c&fipco.faces; by county tract block; run; ===========================End commented out================================ */ %mend doco; %doco(51015,Augusta); %doco(51820,WaynesboroCity); proc sort data=user.facecodesVA out=user.facecodesVA; by county tract block blksfx; run; data user.facecodesVA2; set user.facecodesVA; 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; drop atotal; run; proc sort; by county tract block descending SumAtotal; run; run; *---------Special check step to see if it appears true that when blksfx stays the same so do all the geocodes so we can safely just use the codes on the last obs within that by group ending with blksfx. The test data set covered 2 counties in VA. We found no cases where the if not statement was true. So we conclude from this that blksfx is atomic wrt these geos. ----------* ; data uhoh; set user.facecodesVA; by county tract block blksfx; 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 stop; end; run; /*<=========================Copied from SAS Log window showing interactive run===================================== 77 data uhoh; 78 set user.facecodesVA; by county tract block blksfx; 79 if first.blksfx then do; 80 sdbest14_=sdbest14; sdbesttype14_=sdbesttype14; sldu14_=sldu14; sldl14_=sldl14; cousubfp14_=cousubfp14; 80 ! placefp14_=placefp14; 81 retain sdbest14_ -- placefp14_; 82 end; 83 else 84 if not ( 85 (sdbest14_=sdbest14) & (sdbesttype14=sdbesttype14_) & (sldu14_=sldu14) & 86 (sldl14_=sldl14) & (cousubfp14_=cousubfp14) & (placefp14_=placefp14 ) 87 ) 88 then do; output; _nproblems+1; if _nproblems > 20 then stop; 89 end; 90 run; NOTE: There were 13851 observations read from the data set USER.FACECODESVA. NOTE: The data set USER.UHOH has 0 observations and 29 variables. ===========================End commented out================================ */