x cd /pub/data/pl942010/Tools;  %let pgm=pl94_ustrend_datasets;  filename pgm "&pgm..sas";
 *---Now that we have converted all 50 + 1 states to sas data sets we want to create a series of national data sets with specific
   geographic levels.
 ---*;

 libname pl942010 '/pub/data/pl942010';
 libname pl94 (pl942010);
 libname sf12000x '/pub/data/sf12000x' access=readonly;
 libname sf1 (sf12000x);
 libname popests '/pub/data/popests' access=readonly;
 libname user '/SAS_temp/user';
   title "&pgm.: Create National Trend (2000-2010) Datasets for Selected Geographic Levels";
 %let pctvars=pctwhite1  pctblack1 pctindian1 pctasian1 pcthawnPI1 pctother1 pcthisppop
 pctwhitenh1 pctmultrace pctover18 pctunder18 popPSQMi pctvacant;
 %let wgtvars=Totpop   totpop  totpop   totpop  totpop   totpop  totpop
  totpop   totpop  totpop  totpop   landsqmi  tothus;

 %let pctvars2k=%addsfxs(&pctvars,2k) ;
 %let wgtvars2k=%addsfxs(&wgtvars,2k) ;
  %put pctvars2k=&pctvars2k;

 %macro calcpcts;
   %local npcts i pctvar wtvar _var;  %let npcts=%lenlst(&pctvars2k);
   %do i=1 %to &npcts;
   %let pctvar=%scan(&pctvars2k,&i);
   %let wtvar=%scan(&wgtvars2k,&i);
   %if &pctvar ne %str(popPSQMi2k) %then %do;
   %put pctvar=&pctvar;
   %let _var=%substr(&pctvar,4);
   if &wtvar then &pctvar=100*&_var/&wtvar;
   %end;
   %end;  %*--do i loop--;
  %mend calcpcts;

  options mprint;
 /* ========================begin comment====================================


 *===============================Do counties=============================================================*;
 data pl94.uscnty_trends(index=(state));
 merge
 pl94.uscounties(in=in10)

 sf12000x.uscntys(in=in2k keep=state county totpop totpop90 hisppop white1 black1 indian asian1 hawnpi1 other1 multrace
  white_non_hispanic TotHHs Tot_units Vacant_units over18 landsqmi  PopPsqmi
  rename=(totpop=TotPop2k hisppop=HispPop2k white1=White12k black1=black12k  indian=indian12k
    asian1=asian12k hawnpi1=hawnpi12k other1=other12k multrace=multrace2k
    white_non_hispanic=Whitenh12k TotHHs=OccHUs2k Tot_units=TotHUs2k Vacant_units=Vacant2k over18=Over182k
    landsqmi=LandSQMi2k PopPSQMi=PopPSQMi2k)
  );
 by state county;
 if not in10 then do;
   put '***2k county not matched on 2010: ' county= ;
   delete;
   end;
 if not in2k then put '**No 2k data matched for ' county= ;
 Under182k=totpop2k-over182k;  PctUnder182k=100*Under182k/totpop2k;
 Areaname=trim(Areaname)||', '||upcase(stab);

  %calcpcts


   Change10=TotPop - Totpop2k;  if totpop2k then PctChange10=100*Change10/totpop2k;
   Change20=TotPop - Totpop90;  if totpop90 then PctChange20=100*Change20/totpop90;
  label Change10='Pop Change, 2000-2010'  PctChange10='% Change in Pop, 2000-2010';
  label Change20='Pop Change, 1990-2010'  PctChange20='% Change in Pop, 1990-2010';
   format Pctwhite12k--pctvacant2k 5.1  PctChange: 6.1;
 run;

 *===============================Do places=============================================================*;
 proc sort data= popests.ussc09(keep=sumlev state placefp popest popestBase rename=(popest=popestJL09)
  where=(sumlev='162')) out=popestsplaces(drop=sumlev);
  by state placefp;
  run;
 data pl94.usplace_trends(index=(state));
 merge
 pl94.usplaces(in=in10  drop=County cnty: MetDiv CSA CBSA: where=(sumlev='160') )
 popestsplaces(in=inests)
 sf12000x.usplaces(in=in2k keep=sumlev state placefp areaname totpop totpop90 hisppop white1 black1 indian asian1 hawnpi1 other1
  multrace white_non_hispanic TotHHs Tot_units Vacant_units over18 landsqmi  PopPsqmi
  rename=(totpop=TotPop2k hisppop=HispPop2k white1=White12k black1=black12k  indian=indian12k
    asian1=asian12k hawnpi1=hawnpi12k other1=other12k multrace=multrace2k
    white_non_hispanic=Whitenh12k TotHHs=OccHUs2k Tot_units=TotHUs2k Vacant_units=Vacant2k over18=Over182k
    landsqmi=LandSQMi2k PopPSQMi=PopPSQMi2k areaname=areaname2k)
  where=(sumlev='160')
  );
 by state placefp;
 if not in10  then do;
   put '***2k place not matched on 2010: ' state= placefp= areaname2k= ;
   delete;
   end;
 if not in2k  and lsadc ne '57' then put '**No 2k data matched for ' state= placefp= areaname= ;

 if in2k and (totpop2k > 0)  then do;
   Under182k=totpop2k-over182k;  PctUnder182k=100*Under182k/totpop2k;
   drop areaname2k;
  %calcpcts
   end;
 if in2k or inests then do;
  *---If we have an estimates obs for the place we check use the adjusted 2k pop figure popestbase to calculate total pop change vars--;
  if inests then  _totpop2k=popestbase;  else _totpop2k=Totpop2k;
   Change10=TotPop - _Totpop2k;  if _totpop2k then PctChange10=100*Change10/_totpop2k;
   Change20=TotPop - Totpop90;  if totpop90 then PctChange20=100*Change20/totpop90;
   end;
 Areaname=trim(Areaname)||', '||upcase(stab);
 drop _: ;
  label Change10='Pop Change, 2000-2010'  PctChange10='% Change in Pop, 2000-2010';
  label Change20='Pop Change, 1990-2010'  PctChange20='% Change in Pop, 1990-2010';
   format Pctwhite12k--pctvacant2k 5.1  PctChange: 6.1;
 run;


 *===============================Do cbsas (metro areas)=============================================================*;
 *---lets experiment with doing trends for the cbsas by grabbing some 2k data from the sf12000x dataset---;
 proc sort data=pl94.uscnty_trends  out=aggin_county2;  by cbsa state;  run;
 options nomprint;

  %agg(setin=aggin_county2,setout=aggout,
   aggby=cbsa state,
   agglvl=2, grand=0,
   aggvars=_numeric_,idvars=csa,
   means=&pctvars &pctvars2k pctchange10 pctchange20, mweights=&wgtvars &wgtvars2k totpop2k totpop90,
   debug=0);
 proc sort;  by cbsa state;  run;

  data pl94.uscbsa_trends (label='US CBSAs: Metropolitan/Micropolitan Stat Areas Trend Data');
 length SumLev $3 Geoid  $12 esriid $7 AreaName $80 cbsatype $5;
 set aggout;
 if _lvl_= 2 then do;
   SumLev='310';  Geoid='31000US'||cbsa; esriid=cbsa;
   if cbsa='99999' then delete;
   else Areaname=put(cbsa,$cbsa.);
   if index(areaname,'Micropolitan') then cbsatype='micro';  else cbsatype='metro';
   end;
 if _lvl_= 1 then do;
   SumLev='311';  Geoid='31100US'||cbsa||state;  esriid=cbsa||state;
   if cbsa='99999' then areaname='Non-CBSA remainder of '||put(state,$state.);
   else Areaname=trim(put(cbsa,$cbsa.))||' / ' || put(state,$fipstab.);
   if cbsa ne '99999'  then if index(areaname,'Micropolitan') then cbsatype='micro';  else cbsatype='metro';
   end;
 format cbsa $5.; *--it was $cbsa.  on the uscounties data set--;

 drop _: Ngeo_id LogRecNo;
 drop IntPt: ;  *<----cannot aggregate these----;
 run;
 proc sort;  by cbsa state ;  run;
   ==========================end comment====================================== */

 *===============================Do states and nation ===================================================*;
  data us10;  set pl94.usregdiv(obs=1);  state='  ';  run;
 data pl94.usstate_trends(label='U.S. Totals and States');
 merge
 us10(in=in10us )
 pl94.usstates(in=in10)

 sf12000x.us(in=in2k keep=sumlev state county totpop totpop90 hisppop white1 black1 indian asian1 hawnpi1 other1 multrace
  white_non_hispanic TotHHs Tot_units Vacant_units over18 landsqmi  PopPsqmi
  rename=(totpop=TotPop2k hisppop=HispPop2k white1=White12k black1=black12k  indian=indian12k
    asian1=asian12k hawnpi1=hawnpi12k other1=other12k multrace=multrace2k
    white_non_hispanic=Whitenh12k TotHHs=OccHUs2k Tot_units=TotHUs2k Vacant_units=Vacant2k over18=Over182k
    landsqmi=LandSQMi2k PopPSQMi=PopPSQMi2k)
   where=(sumlev in ('010','040'))
  );
 by state ;
 if in10us then in10=1;
 if not in10 then do;
   put '***2k state not matched on 2010: ' state= ;
   delete;
   end;
 if not in2k then put '**No 2k data matched for ' state= ;
 Under182k=totpop2k-over182k;  PctUnder182k=100*Under182k/totpop2k;

  %calcpcts


   Change10=TotPop - Totpop2k;  if totpop2k then PctChange10=100*Change10/totpop2k;
   Change20=TotPop - Totpop90;  if totpop90 then PctChange20=100*Change20/totpop90;
  label Change10='Pop Change, 2000-2010'  PctChange10='% Change in Pop, 2000-2010';
  label Change20='Pop Change, 1990-2010'  PctChange20='% Change in Pop, 1990-2010';
   format Pctwhite12k--pctvacant2k 5.1  PctChange: 6.1;
 run;


 
