x cd /pub/data/georef/Tools; %let pgm=county_adjacency; filename pgm "&pgm..sas"; *--Process the Census Bureaus couty adjacency file. As of 2010. Downloaded from https://www.census.gov/geo/reference/county-adjacency.html . jgb, Nov 2016 --*; filename txtin "/pub/data/georef/county_adjacency2010.txt"; libname georef '/pub/data/georef'; options compress=yes; data georef.adjacent_counties2010(label="Each obs is one 2010 county adjacency pair"); infile txtin dsd dlm='09'x firstobs=1 ; length cntyname1 cntyname2 $80 fips1 fips2 $5 ; length CountyNm $80 County $5 State $2 Countynm2 $80 County2 $5 State2 $2; input cntyname1 fips1 cntyname2 fips2; retain CountyNm--State; if fips1 ne ' ' then do; County=fips1; State=substr(county,1,2); CountyNm=cntyname1; end; county2=fips2; Countynm2=cntyname2; state2=substr(county2,1,2); OUTPUT; keep CountyNm -- State2; run; proc sort; by county county2; run; proc print data=_last_(obs=40); id Countynm county; run cancel; *----Now create an alternate version with one obs per county with neighbor codes and names cokkapsed into longer text strings--; data georef.adjacent_counties2010_collapsed(label='Has 1 obs per county with AdjacentFips and AdjacentNames'); *---We do not include the county itself as one of its adjacencies--; set georef.adjacent_counties2010 end=last; by county; length Nadjacent 3 AdjacentFips $100 AdjacentNames $400; retain Adjacent: ; if first.county then do; Nadjacent=0; AdjacentFips=' '; AdjacentNames=' '; end; if county ne county2 then do; Nadjacent+1; *--Edit adjacent cnty name for the string. We get rid of word County and omit State Abbrev unless it is a different state--; if index(countynm2,'County,') then countynm2=substr(countynm2,1,length(countynm2)-11); if state2 ne state then Countynm2=trim(countynm2)||', '||put(state2,fipstab2.); Countynm2=compress(CountyNm2,'"'''); *--remove any quotes in name--; AdjacentFips= catx(' ',AdjacentFips, county2); AdjacentNames= catx(' | ', AdjacentNames, Countynm2); end; if last.county then do; output; retain _maxfips _maxnames; _lfips=length(adjacentfips); if _lfips> _maxfips then _maxfips=_lfips; _lnames=length(adjacentNames); if _lnames> _maxnames then _maxnames=_lnames; if last then put '***longest values of AdjacentFips is ' _maxfips '. Longest value of AdjacentNames is ' _maxnames ; end; keep State County CountyNm Nadjacent AdjacentFips AdjacentNames; run; data _null_; set _last_ (obs=25); put / _all_; run; proc freq data=georef.adjacent_counties2010_collapsed; table State Nadjacent; format state $state16.; title "Frequencies from &syslast"; run;