x cd /mscdc/sasfmats; filename pgm 'Szipdmo.sas'; %let pgm=Szipdmo; %put &pgm; libname zipeq90x '/mscdc/data/zipeq90x'; *--generate a format code that will serve as a lookup table to give distance in miles between any 2 zip codes (that appear in 1990 zip equiv. file), where one of the zips is in Missouri + kc, stl-; *--pgmr: J. Blodgett Missouri State Census Data Center, 1-94--; *<=====this is an aborted project...useful only for parts... the format table would be too large to be practical (30k*30k/2)=========; title 'sasfmats/Szipdmo.sas: Create $zipdmo format table'; options nocenter; proc sort data= zipeq90x.uszips(keep=stab zip xpcentrm ypcentrm msapmsa1 pop) out=uszips; by zip descending pop; *<--a few zips cross state lines. We shall keep the part with largest pop; run; data dstncs(label='proc format cntlin set'); set uszips(where=(stab='MO' or msapmsa1 in ('3760','7040')) ); *--we include the kc/ks and stl/il zips as well--; *--declare and initialize special proc-format-cntlin variables-; retain fmtname 'zipdmo ' hlo ' ' length 8 type 'c'; length start $10 label $8; if _n_=1 then do; *--gen obs to specify other=' ' spec--; hlo='o'; *<--alpha "oh", not zero--; label=' '; output; hlo=' '; end; start=zip; *--inner loop selects ALL zips in u.s. for distance calc - except that we only keep 1 pairing of zips that are in the MO+ subset. So we keep 63101,63102 but not 63102,63101 since it would be redundant-; do until( lastzip); set uszips(keep=stab zip xpcentrm ypcentrm msapmsa1 rename=(stab=stab2 zip=zip2 xpcentrm=x2 ypcentrm=y2 msapmsa1= msapmsa2)) end=lastzip; by zip2; if not first.zip2 then continue; *--no dups allowed-; if stab2='MO' or msapmsa2 in ('3760','7040') then if zip2 le zip then continue; *<---lower-triangular filter--; dsq=(xpcentrm-x2)**2 + (ypcentrm-y2)**2; dist=sqrt(dsq); substr(start,6,5)=zip2; *<--table arg is zip||zip2; label=put(dist,8.3); *<--table result is distance in miles; output; end; *<---inner loop; keep fmtname start label; *<--key vars used for a cntlin dataset; keep length type hlo; run; *-----------------------------------------------------------------*; *--use sas data set to create permanent format code, $zipdmo in the library.formats catalog (library libref assumed to be defined); proc format cntlin=dstncs library=library; run; options obs=50; *<---to see only the first 50 entries---; proc format fmtlib library=library; select $zipdmo; title2 'Partial print of entries in $zipdmo format code'; run; *-----------------------------------------------------------------*; *--data step to test and demonstrate how to use the format table-; data zipdists; infile cards eof=part2; length zip $5; array zips(20) $5 _temporary_; do while (1); input zip $ @; _i_+1; zips(_i_)=zip; end; return; part2: *--generate sas data set with all zip combinations and distances; n=_i_; put n ' ZIPs to process...'; length arg $10 distance 8; do _i_= 1 to n; zip=zips(_i_); arg=zip; do _j_=1 to n; zip2=zips(_j_); if zip2 le zip then continue; substr(arg,6,5)=zip2; distance=put(arg,$zipdmo.); *--our format only works when one of the zips in Mo or kc or stl; if distance ne . then output; end; *--_j_ loop--; end; *--_i_ loop--; keep zip zip2 distance; format distance 8.3; cards; 63101 63105 62001 27513 01001 65801 67701 90101 run; proc print data=zipdists; title2 'zipdists'; run;