x cd /pub/data/georef/Tools; %let pgm=cnvt_free_zipcode_database; filename pgm "&pgm..sas"; *---Read copy of this ZIP code database as downloaded from web site: http://federalgovernmentzipcodes.us/ June 21, 2010. We downloaded both the CSV and xlsx versions. Discovered that the CSV file had only 2 digits of accuracy on the lat-long fields so we downloaded the xlsx file and exported to the CSV file we are reading here. Running 11-16-10 with revised downloaded CSV file (now with 6 digits of lat-long accuracy). ---; title "&pgm.: Convert the Free ZIP code File from federalgovernmentzicpodes.us"; *filename in "/pub/data/georef/Copy of free_zipcode_database.csv"; filename in "/pub/data/georef/free_zipcode_database_nov2010.csv"; libname georef "/pub/data/georef"; data zipcodes; infile in dsd dlm=',' termstr=crlf missover lrecl=255 firstobs=2; length ZIP $5 Lat Long 8 City $32 Stab $2 CntyName $24 Ziptype $16 Primary $3 _WorldRegion $8 _Country $8 Location $32 _location2 $48 Pop HUs Income 5 Income_rank 3 LandArea WaterArea 5 Decommissioned $3 _MilitaryRestrictions $8; input zip -- Income LandArea -- _MilitaryRestrictions; length State $2; *** if stab='MO'; *<================; retain state; if stab ne lag(stab) then do; if stab in ('AA','AE','AP',' ') then state=' '; else State=put(stab,$stabfip.); if state=stab then put state = stab= " ***state lookup failed*** "; end; if primary='YES' then primary='Yes'; if ziptype =:'r' then ziptype=substr(ziptype,2); if ziptype='Unique' then Ziptype='UNIQUE'; if decommissioned='NO' then decommissioned="No"; format Lat Long 11.6 pop hus comma7. income dollar8. Landarea 8.3 WaterArea 7.4; drop _:; run; proc sort; by zip primary; run; *---We want to get rid of the non-primary records/obs because they just duplicate the primary data except for the location name. We keep all those alternate names in a new char varibles, altnames.---; data zipcodes2; set zipcodes; by zip; length altnames $128 Naltnames 3; retain altnames Naltnames; if first.zip then do; Naltnames=0; Altnames=' '; end; if first.zip then do; if not last.zip then do; altnames=scan(location,1,','); Naltnames=1; end; end; else if primary='No' and index(altnames,trim(location))=0 then do; *--if the altnames string has not already seen this name... --; Naltnames+1; altnames=trim(altnames)||', '||scan(location,1,','); end; if last.zip then output; drop primary; run; proc rank data=zipcodes2(keep=zip income where=(income)) out=ranks groups=100 ; var income; ranks income_rank; run; data georef.zipcodes (label="From free_zipcode_database at federgovernmentzipcodes.us 11/2010" compress=Yes index=(state)); merge zipcodes2 ranks(keep=zip income_rank); by zip; label Pop='2000 Census Pop (100%)' HUs='2000 Census Hsng Units' Income='Medn Family Incm - 1999'; label income_rank="Pctile Rank - Medn Fmly Incm"; format income_rank best4.; run; ods listing close; ods html file="&pgm..html" style=minimal; proc freq data=georef.zipcodes; table State*Stab / list missing ; table Ziptype Decommissioned; title2 "Some Frequencies"; run cancel; options linesize=160; proc print data=georef.zipcodes(where=(stab='MO') drop=city decommissioned); by stab state; id zip; title2 "Missouri Subset"; run; ods html close;