x cd /pub/data/corrlst/Tools;  %let pgm=us_stzcta5_county;  filename pgm "&pgm..sas";
*---Create various ZCTA to county correlation list datasets and generate csvfiles with "primary" counties for ZCTAs.
  Running for entire us, 10-01.
  Uses sf12000 headers data stored in sf12000/xxgeos directory as source.

  John Blodgett, OSEDA, U. of Missouri Outreach & Extension
 ---*;

 title 'Creating a National ZCTA to County Correlation List';
 options nodate;

libname user '/tmp/storage/john';
%libname(sf12000,xxgeos);
libname corrlst '/pub/data/corrlst';

%macro geoset(state,stab);
  xxgeos.&stab.geos(keep=state stab county zcta5 pop100 sumlev)
  %mend geoset;

data us881;
  set
  %dostates(n=51,mac=geoset)
  open=defer;
  where sumlev='881' and pop100 and substr(zcta5,4,2) not in ('HH','XX');
  drop sumlev;
  run;
proc sort;  by state zcta5;  run;
%corrwt(setin=us881,setout=corrwtout, g1=state zcta5,g2=county,wtvar=pop100,keepwt=1) ;
proc sort; by state zcta5 descending pop100; run;

data corrlst.us_stzcta5_county(label='US State-ZCTA5 to County Corr. List, 2K Census' index=(state) );
  set corrwtout;
  length rank 3;  *--rank=1 means it is the primary county, rank=2 says it has the 2nd largest portion of the zcta pop,etc.;
  fipco=county;  format county $county.;
  by state zcta5;
  if first.zcta5 then rank=1; else rank+1;
  label pop100='Census 2k 100% Pop in ZCTA/County intersection'
    afact='Portion of Census 2k Pop of ZCTA in this County';
  length stab $2;  retain stab;
  if first.state then stab=put(state,$fipstab.);
  format afact 5.3 pop100 comma6.;
  run;

proc print data=corrlst.us_stzcta5_county(obs=250);
  title 'Sample Observations from corrlst.us_stzcta5_county: Related state/zcta5 to counties for US';
  by state stab;
  id zcta5;
  run;


 
