x cd /pub/data/irsmig/Tools; %let pgm=agg2cbsa_state; filename pgm "&pgm..sas"; *---Per request from Tim Alexander, St Louis RCGA: agg the migration data for a specified CBSA/State (St. Louis CBSA, done for the Mo and Il sides) with the origin/destination being at the cbsa/state level. These numbers will be inherhently wrong (low) because of the suppression of the less-than-10-moves counties. But the numbers are still useful indicators of where migration is coming from and going to for the metro area. jgb, 8-22-07. ---; %libname (irsmig); libname temp '/pub/data/temp'; *---output is only temporary for now----------; title "&pgm.: Aggregate IRS Migration Data to Get Moves at State and CBSA Levels"; libname user '/SAS_temp/user'; %macro doit(years=0506,state=29,cbsa=41180,cbsaname=%str(St. Louis Metropolitan Statistical Area)); /* =======================Begin comment================ %let years=0506; %let state=29; %let cbsa=41180; %let cbsaname='St. Louis MSA'; ======================End comment================== */ data countyin; *---moves from outside the state/cbsa into the state/cbsa. Ready to aggregate--; set irsmig.usinmig&years; where ToState="&state" and ToSumlev='050' and FromSumlev='050'; length ToCbsa $5 FromCbsa $5; if put(ToCounty,$cocbsa.)="&cbsa"; ToCBSA="&cbsa"; FromCBSA=put(FromCounty,$cocbsa.); if not (FromState="&state" and FromCBSA="&cbsa"); *<---if they were already in this state/cbsa then not in-migrants--; if FromCBSA=' ' then FromCBSA='99999'; keep ToCBSA ToState FromCBSA FromState NReturns Nexemptions TotalIncome AvgHHIncome PerCapitaIncome; run; proc sort; by FromState FromCBSA; run; %agg(aggin=countyin, aggby=FromState FromCBSA, idvars=ToState ToCBSA, aggvars=Nreturns Nexemptions TotalIncome AvgHHIncome PerCapitaIncome, means=AvgHHIncome PerCapitaIncome, mweights=Nreturns Nexemptions, agglvl=2) run; data temp.irsmig_&state&cbsa._&years.In(label="Inmigration to &cbsaname / State &state: From State/CBSA levels for years &years"); set aggout; length FromAreaname $64; if _lvl_=1 then do; FromSumlev='320'; FromAreaname=trim(put(FromCbsa,$cbsa.))|| ' / '||put(FromState,$fipstab.); end; else if _lvl_=2 then do; FromSumLev='040'; FromAreaname=put(FromState,$state.); end; drop _: ; format Avg: Per: dollar9. TotalIncome dollar10.; format Tostate $state. Tocbsa $cbsa.; run; proc print data=temp.irsmig_&state&cbsa._&years.In (obs=40); title2 "temp.irsmig_&state&cbsa._&years.In"; by ToState ToCBSA; id FromState FromCBSA FromAreaname; run; *=========================================Now to outmigrants==============================================; data countyout; *---moves from within the state/cbsa other state/cbsa. Ready to aggregate--; set irsmig.usoutmig&years; where FromState="&state" and ToSumlev='050' and FromSumlev='050'; length ToCbsa $5 FromCbsa $5; if put(FromCounty,$cocbsa.)="&cbsa"; FromCBSA="&cbsa"; ToCBSA=put(ToCounty,$cocbsa.); if not (ToState="&state" and ToCBSA="&cbsa"); if ToCBSA=' ' then ToCBSA='99999'; keep FromCBSA FromState ToCBSA ToState NReturns Nexemptions TotalIncome AvgHHIncome PerCapitaIncome; run; proc sort; by ToState ToCBSA; run; %agg(aggin=countyout, aggby=ToState ToCBSA, idvars=FromState FromCBSA, aggvars=Nreturns Nexemptions TotalIncome AvgHHIncome PerCapitaIncome, means=AvgHHIncome PerCapitaIncome, mweights=Nreturns Nexemptions, agglvl=2) run; data temp.irsmig_&state&cbsa._&years.Out(label="Outmigration from &cbsaname / State &state: To State/CBSA levels for years &years"); set aggout; length ToAreaname $64; if _lvl_=1 then do; ToSumlev='320'; ToAreaname=trim(put(ToCbsa,$cbsa.))|| ' / '||put(ToState,$fipstab.); end; else if _lvl_=2 then do; ToSumLev='040'; ToAreaname=put(ToState,$state.); end; drop _: ; format Avg: Per: dollar9. TotalIncome dollar10.; format Fromstate $state. Fromcbsa $cbsa.; run; proc print data=temp.irsmig_&state&cbsa._&years.Out (obs=40); title2 "temp.irsmig_&state&cbsa._&years.Out"; by FromState FromCBSA; id ToState ToCBSA ToAreaname; run; %mend doit; options NOmprint; %doit(state=29) ; options nomprint; %doit(state=17) ;