x cd /pub/data/gazetteer/Tools; %let pgm=cnvt20xx; filename pgm "&pgm..sas"; /* Read gazetteer text files (from the Bureau) and store variables using our naming conventions (stab instead of usps, for example). Add sumlev variable and call the geoid field esriid to avoid confusion with the longer geoIDs. */ options ls=157 nodate; title "US Census gazetteer data sets, converted on &sysdate"; libname gazetteer '/pub/data/gazetteer'; %let unzipal =1; %let vintage=2013; x 'cd /pub/data/gazetteer/rawdata'; *---where the input data resides---; %macro cnvttxt(filename, /* the input filename such as counties, tracts, etc. Case sensitive */ sumlev, /* 3-digit summary level code */ unzip=0, /* set to 1 if the txt file needs to be unzipped from the corr .zip file */ debug=0); %*--This macro reads one of the gazetteer input txt files and creates the corresponding SAS data set--; %if &vintage=2010 and (&filename=aiannh or &filename=ua) %then %let national=%str(); %else %let national=_national; %if &unzipall=1 %then %let unzip=1; %if &unzip %then %do; %if &vintage=2010 %then %str( x "unzip Gaz_&filename.&national..zip";); %else %str( x "unzip &vintage._Gaz_&filename._national.zip"; ); %end; %if &vintage=2010 %then %let filen=%unquote(Gaz_&filename.&national..txt); %else %let filen= %str(&vintage._Gaz_&filename._national.txt); filename indata "&filen"; %if %substr(&filename,%length(&filename),1) ne s %then %let _s=s; %else %let _s=%str(); %*---assign the cd set name for consistency with 2012 and later--; %if &vintage=2010 and &filename=cd111 %then %let setname=us111cds2010; %else %let setname=us&filename.&_s.&vintage; %let setname=%unquote(&setname); data _null_ ; *---Read the header line---; infile indata dsd dlm='09'x missover termstr=&termstr obs=1; length line $200; input; linein=line; line=upcase(_infile_); line=translate(line,' ','09'x); Firstword=scan(line,1); state=(firstword='USPS'); call symput('state',put(state,1.)); name=(index(line,'NAME')>0); call symput('name',put(name,1.)); ansicode=(index(line,'ANSICODE')>0); call symput('ansicode',put(ansicode,1.)); funcstat=(index(line,'FUNCSTAT')>0); call symput('funcstat',put(funcstat,1.)); lsad=(index(line,'LSAD')>0); call symput('lsad',put(lsad,1.)); line=tranwrd(line,'FUNCSTAT10','FUNCSTAT'); line=tranwrd(line,'USPS','Stab'); line=tranwrd(line,'GEOID','esriid'); line=tranwrd(line,'ALAND_SQMI','LandSQMI'); line=tranwrd(line,'AWATER_SQMI','WaterSQMI'); line=tranwrd(line,'INTPTLONG','IntPtLon'); line=tranwrd(line,'INTPTLAT','IntPtLat'); line=tranwrd(line,'NAME','AreaName'); if &debug then put linein= / line= +2 ' (edited)'; call symput('varsin',trim(line)); stop; run; %put _user_; data gazetteer.&setname ; *--The length statement assigns length and controls order of variables within the observation---; length %if &state %then %str( State $2 Stab $2 ); esriid $11 %if &ansicode %then %str( ANSICODE $8 ); %if &name %then %str( Areaname $96 ); %if &funcstat %then %str( FUNCSTAT $2 ); %if &lsad %then %str( LSAD $2 ); %if &sumlev=400 %then %str(UATYPE $1 ); %if %substr(&filename,3,2)=sd %then %str (LOGRADE HIGRADE $2 ); ; %if &lsad %then %str( label lsad='Legal/Statistical area descriptor';); infile indata dsd dlm='09'x termstr=&termstr missover firstobs=2; input &varsin ; %if &sumlev=050 %then %do; %end; %else %if &sumlev=060 %then %do; length County $5 Cnty $3; County=substr(esriid,1,5); Cnty=substr(County,3,3); format County $county.; %end; %else %if &sumlev=140 %then %do; length Atract $7 County $5 Cnty $3; label Atract='Census Tract'; format atract $char7.; County=substr(esriid,1,5); Cnty=substr(County,3,3); format County $county.; length _tract1 _tract2 8; _tract1=substr(esriid,6,4); _tract2=substr(esriid,10,2); if substr(esriid,10,2)='00' then Atract=put(_tract1,4.); else Atract=put(_tract1,4.)||'.'||put(_tract2,z2.); drop _tract1 _tract2; %end; %else %if &sumlev=860 %then %do; length ZIPname $40; ZIPname=put(esriid,$zipnmus.); label zipname='Unofficial ZIP Name'; length Pstate Pstab $2; label Pstate='Primary State (FIPS)' Pstab="Primary State (USPS)"; pstab=upcase(put(esriid,$zipstab.)); if pstab=' ' and esriid=:'006' then pstab='PR'; pstate=put(pstab,$stabfip2.); %end; %else %if &sumlev=400 %then %do; length States $14; label States="State(s)"; length Nstates 3; label Nstates='# States'; if uatype='C' then _ix=index(areaname,'Urban Cluster'); else _ix=index(areaname,'Urbanized Area'); if _ix then do; *--The name ends with state postal separated by double dashes, e.g. MA--NH--RI ---; States=left(scan(substr(areaname,1,_ix-2),-1,',')); _lstates=length(States); if _lstates=2 then nstates=1; else nstates=1+ (_lstates-2)/4; end; drop _ix _lstates; %end; %*---add code unique to the summary level (geographic type)----; %if &state %then %str( State=substr(esriid,1,2);); AreaSQMI=LandSQMI + WaterSQMI; label AreaSQMI="Total Area Sq Mls" LandSQMI="Land Area Sq Mls"; label aland='Land Area SqKM' awater='Water Area SqKM'; format intptlat intptlon 11.6 LandSQMI WaterSQMI AreaSQMI 9.3; retain sumlev "&sumlev"; retain vintage "&vintage"; run; filename indata clear; proc print data=gazetteer.&setname(obs=20); by sumlev vintage; title2 "First 20 Obs on gazetteer.&setname"; id esriid; run; %mend cnvttxt; options mprint; /*<=========================Comment out===================================== %let vintage=2012; %let termstr=crlf; %cnvttxt(counties,050,unzip=1); %cnvttxt(place,160,debug=0); %cnvttxt(cousubs,060,debug=0); %cnvttxt(aiannh,250) %cnvttxt(112CDs,500) %cnvttxt(elsd,950) %cnvttxt(scsd,960) %cnvttxt(unsd,970) %cnvttxt(sldu,610) %cnvttxt(sldl,620) %cnvttxt(tracts,140) %cnvttxt(zcta,860) %cnvttxt(ua,400) proc print data=gazetteer.usuas&vintage(where=(index(areaname,'--'))); title2 "usuas&vintage With Multiple States"; by sumlev vintage; id esriid; run; %let vintage=2013; %let termstr=crlf; %let unzipall=1; %cnvttxt(counties,050,unzip=1); %cnvttxt(place,160,debug=0); %cnvttxt(cousubs,060,debug=0); %cnvttxt(aiannh,250) %cnvttxt(113CDs,500) %cnvttxt(elsd,950) %cnvttxt(scsd,960) %cnvttxt(unsd,970) %cnvttxt(sldu,610) %cnvttxt(sldl,620) %cnvttxt(tracts,140) %cnvttxt(zcta,860) %cnvttxt(ua,400) proc print data=gazetteer.usuas&vintage(where=(index(areaname,'--'))); title2 "usuas&vintage With Multiple States"; by sumlev vintage; id esriid; run; %let vintage=2014; %let termstr=lf; %let unzipall=1; %cnvttxt(counties,050,unzip=1); %cnvttxt(place,160,debug=0); %cnvttxt(cousubs,060,debug=0); %cnvttxt(aiannh,250) %cnvttxt(114CDs,500) %cnvttxt(elsd,950) %cnvttxt(scsd,960) %cnvttxt(unsd,970) %cnvttxt(sldu,610) %cnvttxt(sldl,620) %cnvttxt(tracts,140) %cnvttxt(zcta,860) %cnvttxt(ua,400) proc print data=gazetteer.usuas&vintage(where=(index(areaname,'--'))); title2 "usuas&vintage With Multiple States"; by sumlev vintage; id esriid; run; ===========================End commented out================================ */ %let vintage=2010; %let termstr=crlf; %let unzipall=1; /*<=========================Comment out===================================== %cnvttxt(counties,050,unzip=1); %cnvttxt(places,160); *--note plural--; %cnvttxt(cousubs,060); %cnvttxt(aiannh,250) %cnvttxt(cd111,500) %cnvttxt(elsd,950) %cnvttxt(scsd,960) %cnvttxt(unsd,970) %cnvttxt(sldu,610) %cnvttxt(sldl,620) %cnvttxt(tracts,140) %cnvttxt(zcta,860) ===========================End commented out================================ */ %cnvttxt(ua,400) *--Note that it was Gaz_ua.zip but inside it was Gaz_ua_national.txt so we renamed it manually to Gaz_ua.txt-; options ls=157; proc print data=gazetteer.usuas&vintage(where=(index(areaname,'--'))); title2 "usuas&vintage With Multiple States"; by sumlev vintage; id esriid; run; ods html file= "/pub/data/gazetteer/ContentsReport.html"; options mprint; %include sasmacro(contents.sas); %contents(gazetteer._all_,directory=2); run; ods html close;