x cd /pub/data/irsmig/Tools; %let pgm=irsmig_profile; filename pgm "&pgm..sas"; *---generate a profile report for a single geographic area -- state or county. Options to specify what years, what levels, etc. Idea is to bring together all the data scattered throughout many datasets to get the picture for just this one geographic area. jgb, April, 2007. ---*; title "&pgm"; %libname(irsmig); %macro irspro(st=29,co=,yrs=00 01 02 03 04 05,adjust=0,debug=0,minreturns=10); %if &st=29 %then %let geoset=mo; %else %let geoset=us; %local yr years; %let insets=; %let outsets=; %let i=1; %let yr=%scan(&yrs,1); %do %while(&yr ne %str()); %if &yr=00 %then %let years=9900; %else %let years=0%eval(&yr-1)&yr; %let insets=&insets irsmig.&geoset.inmig&years; %let outsets=&outsets irsmig.&geoset.outmig&years; %let i=%eval(&i+1); %let yr=%scan(&yrs,&i); %if &i=9 %then %goto endmac; %*--guard against looping---; %end; data irsproIN; length Years $9 _year1 _year2 $4; set &insets open=defer; where ToState="&st" and ToCnty="&co"; length FromToCode $5 FromToName $85;; if FromSumlev='tfo' and FromAreaName=:"Other" then delete; ***if FromSumlev=:'t' then FromToCode=FromState; FromToCode=FromCounty; if substr(years,1,2) < '20' then _year1='20'||substr(years,1,2); else _year1='19'||substr(years,1,2); if substr(years,3,2) < '20' then _year2='20'||substr(years,3,2); else _year2='19'||substr(years,3,2); Years=_year1||'-'||_year2; FromToName=FromAreaName; if _N_=1 then do; call symput('anchorname',trim(ToAreaName)); call symput('anchorcode',trim(ToCounty)); end; keep Years FromTo: NReturns--PerCapitaIncome; drop TotalIncome; rename AvgHHIncome=AvgHHIncomeIn MedianIncome=MedianIncomeIn Nexemptions=NexemptionsIn Nreturns=NreturnsIn; run; proc sort; by Years FromToCode; run; %if &debug %then %do; proc print data=irsproIN ; by years notsorted ; run; %end; run; data irsproOUT; length Years $9 _year1 _year2 $4; set &outsets open=defer; where FromState="&st" and FromCnty="&co"; length FromToCode $5 FromToName $85; if ToSumLev='tfo' and ToAreaName=:"Other" then delete; ***if ToSumlev=:'t' then FromToCode=ToState; FromToCode=ToCounty; if substr(years,1,2) < '20' then _year1='20'||substr(years,1,2); else _year1='19'||substr(years,1,2); if substr(years,3,2) < '20' then _year2='20'||substr(years,3,2); else _year2='19'||substr(years,3,2); Years=_year1||'-'||_year2; FromToName=ToAreaName; keep Years FromTo: NReturns--PerCapitaIncome; drop TotalIncome; rename AvgHHIncome=AvgHHIncomeOut MedianIncome=MedianIncomeOut Nexemptions=NexemptionsOut Nreturns=NreturnsOut; run; proc sort; by Years FromToCode; run; %if &debug %then %do; data _null_; set; by years fromtocode; if not (first.fromtocode and last.fromtocode); put '**dups**' / _all_; run; proc print data=irsproOUT ; by years notsorted ; run; %end; run; data profile; merge irsproIN(in=in1) irsproOut(in=in2); by Years FromToCode; if FromToCode ne "&anchorcode"; *NetReturns=NreturnsIn - NreturnsOut; *GrossReturns=nreturnsin + nreturnsout; length FromTo $80; if '01' le fromtocode le '56' then _group=2; else if fromtocode=:'5' then _group=3; else _group=1; if _group=2 then do; _ix=index(FromToName,'County'); if _ix then FromToName=substr(FromToName,1,_ix-1)|| substr(FromToName,_ix+6); GrossReturns=sum(of nreturnsin, nreturnsout); if GrossReturns ge &minreturns; FromTo=trim(fromtocode)||' '||FromToName;; end; else do; GrossReturns=0; FromTo=FromToName; end; %if &minreturns gt 10 %then %do; if _group=3 then if FromToName=:'Other ' then delete; *<---Because it will no longer be meaningful as an Other cat--; %end; label Years="Tax Years"; run; proc sort; by years _group descending GrossReturns; **** fromtocode; run; options nodate; ods listing close; %*let anchorcode=29019; %*let anchorname=Boone MO; title "County Migration Profile for &anchorname (&anchorcode)"; %if &minreturns gt 10 %then %do; title2 .h=1 "Note: Counties Where Returns Into + Returns Out of Total < &minreturns Are Not Shown"; %end; footnote .h=1 .c=black "Source: IRS Migration Files"; footnote2 .h=1 .c=black "County level Entries are Sorted by Gross Returns (Returns Into + Returns Out Of)"; footnote3 .h=1 .c=black "Missing Data (.) Indicates Fewer than 10 Returns"; footnote4 .h=1 .c=grey "Produced on &sysdate at &systime by setup in irsmig/Tools/irsmig_profile.sas"; ods html file="../test_profile.html"(title="County Migration Profile: &anchorname") style=sasweb; proc report data=profile nofs headskip headline style(column)=[foreground=black background=white font_face=helvetica font_size=2 font_weight=bold ] style(lines)=[foreground=black background=lightgrey font_face=arial font_weight=bold font_size=2]; by years; column FromTo ("Migration Into &anchorname" nreturnsIN nexemptionsIN medianincomein avghhincomein) ("Migration Out of &anchorname" nreturnsOut nexemptionsOut medianIncomeout avghhincomeOut) ; define FromTo / ID width=30 "Migration Flow (From|To) Area" style=[foreground=black font_size=2 font_face=arial outputwidth=160 ]; ***style=[background=#770077 foreground=white font_size=2 font_face=arial outputwidth=160 ]; define nreturnsin /"# Returns" format=comma7. ; define nexemptionsin /"# Exemptions" format=comma9.; define medianincomein / "Median Adjusted Gross Income" format=dollar9.; define avghhincomeIn / "Avg Adjusted Gross Income" format=dollar9.; define nreturnsOut / "# Returns" format=comma7.; define nexemptionsOut / "# Exemptions" format=comma9.; define medianincomeOut / "Median Adjusted Gross Income" format=dollar9.; define avghhincomeOut / "Avg Adjusted Gross Income" format=dollar9.; run; ods html close; %endmac: %mend irspro; options mprint; %irspro(st=05,co=019,yrs=05,minreturns=10,debug=0); %*irspro(co=019,yrs=00 01 02 03 04 05,debug=0);