x cd /pub/data/corrlst/Tools; %let pgm=zip07_cbsa06; filename pgm "&pgm..sas"; *---Read the Census Bureau "geographic relationship" file as downloaded from link at http://www.census.gov/population/www/metroareas/metroarea.html . We do not read all the name fields in order to keep the size of the data set more reasonable. jgb, OSEDA/MCDC 9/2009. ---*; libname corrlst '/pub/data/corrlst'; libname user '/SAS_temp/userb'; filename in pipe 'unzip -p ../zip07_cbsa06'; filename out 'zip07_cbsa06_first50.txt'; title "Create corrlst.&pgm : Linking 2007 ZIP geography to 2006 CBSA Codes"; data work.&pgm(label='2007 ZIP to CBSA (2006) equivalency' compress=yes); infile in dsd obs=max lrecl=512 firstobs=2; length ZIP5 $5 ZIPsfx $4 _zip9 $9 State Stab $2 _cnty $3 _cntyname $100 CBSA $5 _cbsattitle $160 CBSAType $5; length MetroDiv $5 _metrotitle $100 _metrodivtype $5 CSA $3 ; label MetroDiv='Metropolitan Division' CSA='Combined Stat Area'; input zip5 -- CBSAType MetroDiv--CSA; if _n_ le 50 then do; file out; put _infile_; end; if length(zip5) ne 5 then do; _nbad+1; if _nbad ge 50 then abort abend; else list; delete; end; if _n_ le 5 then list; length County $5 CBSAName $5; County=State||_cnty; format county $county.; CBSAName=cbsa; format CBSAName $cbsa.; MetroDivName=MetroDiv; format MetroDivName $metrodiv.; CSAName=CSA; format csaname $csa.; attrib lsfx label='ZIP sfx length' length=3; if length(_zip9)=7 then ZIPsfx=substr(_zip9,6,2); if ZIPsfx=' ' then lsfx=0; else lsfx=length(ZIPsfx); _prevzip=lag(zip5); _prevsfx=lag(zipsfx); InSequence= (zipsfx ne ' ' and zip5=_prevzip and zipsfx=(_prevsfx+1)); drop _: ; run; proc sort data=work.&pgm out=user.&pgm; by state zip5 ; run; proc print data=&pgm(obs=500); by state zip5; id state zip5; var zipsfx lsfx county cbsa metrodiv csa InSequence; run; data corrlst.&pgm(label='2007 ZIP to CBSA (2006) equivalency' compress=yes); length State $2 ZIP5 $5 ZIPsfxs $9; set &pgm; label ZIPsfxs='ZIP suffix(es)'; by state zip5 lsfx notsorted county notsorted cbsa notsorted metrodiv notsorted csa notsorted InSequence notsorted; if first.csa or InSequence=0 then do; ZIPsfxs=zipsfx; _endsfx=' '; length _cursfx 8; _cursfx=zipsfx; end; else if InSequence then do; _endsfx=ZIPsfx; _cursfx+1; end; retain ZIPsfxs _endsfx _cursfx; if last.csa or (Insequence and last.insequence) then do; if _endsfx ne ' ' then ZIPsfxs=trim(ZIPsfxs)||'-'||_endsfx; output; end; drop _: zipsfx InSequence; run; proc datasets library=corrlst nolist; modify &pgm; index create state; index create zip5; quit; proc print data=corrlst.&pgm(obs=150); title2 "First 150 Obs on corrlst.&pgm"; by state zip5; id state zip5; run; proc freq data=corrlst.&pgm; table state*stab/list; table lsfx cbsatype ; run;