x cd /pub/data/irsmig/Tools; %let pgm=cnvt_irsmig; filename pgm "&pgm..sas"; *---Read the IRS migration file. (2003:) The CSV files were created by exporting from the xls spreadsheet we received from David Peters, MERIC (MO Dept of Economic Development). Source of data is US Internal Revenue Service. John Blodgett, Feb. 2003. Purchased complete set of files for US thru 0405 tax years. April, 2007. Now running for later years (04-05). 4-10-07, jgb Bugs fixed and conversion rerun, 4-15-07. Problem found with Ohio Outmig data for 0304 being fixed, 5-9-07. 8-4-07: Processing new data (only for states 01-47) for 05-06 (because of problem with initial cd sent by IRS). 8-11-07: Running again after processing new CD with data for all the states. 8-19-08: Running after processing new 0607 CD with data for all the states. 9-22-08: Rerunning the conversions for most recent 2 years (0506 and 0607) to process all data for Texas. 9-29-08: Rerunning conversion for earlier years (thru 0405) to get complete data for Texas. 10-19-09: Running with data downloaded from IRS web site with 0708 data. 07-25-11: Runnig with data with 0809 cycle. 12-05-11: Runnig with data with 0910 cycle. 04-22-14: Runnig with data with 1011 cycle. Major change in variables related to income. Now just 1: Aggr_AGI (=TotalIncome before). *<=========================================NOTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!======================================== 08-04-15: New files with 2011-12 data in revised format. We go to cnvt_irsmig2.sas to work with these new files. ---; title "&pgm -- Convert Data from IRS on Characteristics of Migrating Tax Returners"; libname irsmig '/pub/data/irsmig'; missing d; %macro addlzs(var); &var=translate(right(&var),'0',' '); %mend addlzs; %macro cnvtfls(years); *---This macro reads input files and creates SAS datasets for a state or for the US as a whole depending on the single positional parameter passed to it.----; /* =======================Begin comment================ %local areaname stab ucstab; %if &mo_us=mo %then %let areaname=Missouri; %else %if &mo_us=us %then %let areaname=United States; %else %do; %put ****Invalid parameter pass to cnvtfls macro: &mo_us ****; %goto endmac; %end; %let stab=&mo_us; %let ucstab=%upcase(&mo_us); ======================End comment================== */ %if &years=0001 %then %do; filename csvin "../co0001_rUSi.csv" ; filename csvinout "../co0001_rUSo.csv"; %end; %else %if &years le 0607 %then %do; filename csvin "../co&years.USi.csv"; filename csvinout "../co&years.USo.csv"; %end; %else %do; filename csvin "../countyinflow&years..csv"; filename csvinout "../countyoutflow&years..csv"; %end; %if &years lt 1011 %then %do; %let pre1011=1; %let numvars=TotalIncome MedianIncome AvgHHIncome PerCapitaIncome ; %let lennumvars=4; %let lastvarin=PerCapitaIncome; %end; %else %do; %let pre1011=0; %let numvars=Aggr_AGI ; %let lennumvars=5; %let lastvarin=Aggr_AGI ; %end; data irsmig.usinmig&years(label="Migrations INTO all US counties based on returns for tax years &years"); infile csvin dsd %if &years ge 0708 %then %str( firstobs=2 ); missover %if &years eq 0910 %then %str(termstr=crlf); ; ; retain Years "&years"; label years='Tax years on which moves are based'; length ToAreaName $64 ToCounty FromAreaName $64 FromCounty $5 ToSumlev FromSumLev $3 ToState $2 ToCnty $3 FromState $2 FromCnty $3 FromStab $2 _namein $64 NReturns NExemptions 4 &numvars &lennumvars. ; label NReturns='# Tax Returns' NExemptions='# Exemptions on Tax Returns' ; %if &pre1011 %then %do; label TotalIncome='Aggregate Adjusted Gross Income ($000)' MedianIncome='Median Gross Income per Return' AvgHHIncome='Average Gross Income Per Return' PerCapitaIncome='Averge Gross Income Per Exemption'; *<--maybe a misleading name----; %end; %else %do; label Aggr_AGI='Aggregate Adjusted Gross Income ($000)' AvgAGI='Avg AGI'; %end; %if &pre1011 %then %str( informat Nreturns--&lastvarin comma.; ); %if &years ge %str(0506) %then %do; input ToState -- &lastvarin ; **drop MedianIncome; if ToState ne ' '; %end; %else %do; input ToState -- &lastvarin; %end; FromAreaName=_namein; drop _: ; length ToStab $2; if ToState > '00' then ToStab=put(ToState,$fipstab2.); %addlzs(ToState) %addlzs(ToCnty) %addlzs(FromState) %addlzs(FromCnty) %if &pre1011 %then %do; AvgHHIncome=round(TotalIncome*1000/Nreturns); PerCapitaIncome=round(TotalIncome*1000/NExemptions); format NReturns Nexemptions comma9. TotalIncome comma11. MedianIncome--PerCapitaIncome dollar8.; %end; %else %do; if Nreturns then AvgAGI=Aggr_AGI*1000/Nreturns; format AvgAGI dollar7. Aggr_AGI comma11.; %end; ToCounty=ToState||ToCounty; if tocnty='000' then select(tostate); when('00') do; ToSumLev='010'; ToAreaName='United States'; end; otherwise do; ToAreaName=put(ToState,$state.); ToSumLev='040'; end; end; else do; ToCounty=ToState||ToCnty; ToAreaName=put(ToCounty,$county.); ToSumlev='050'; end; FromCounty=FromState||FromCnty; Select (FromCounty); when('96000') do; FromSumlev='tot'; FromAreaName='Total Mig - US & Foreign'; end; when('97000') do; FromSumlev='tus'; FromAreaName='Total Mig - US'; end; when('97001') do; FromSumlev='tss'; FromAreaName='Total Mig - Same State'; end; when('97003') do; FromSumlev='tds'; FromAreaName='Total Mig - Different State'; end; when('98000') do; FromSumlev='tfo'; FromAreaName='Total Mig - Foreign'; end; when(ToCounty) do; FromSumlev='050'; FromAreaName='Non-Migrants'; end; when('57005') do; FromSumlev='apo'; FromAreaName='Foreign - APO/FPO ZIPS'; end; when('58000') do; FromSumlev='oss'; FromAreaName='Other Flows - Same State'; end; when('59000') do; FromSumLev='ods'; FromAreaName='Other Flows - Different State'; end; when('59001') do; FromSumLev='ore'; FromAreaName='Other Flows - Northeast'; end; when('59003') do; FromSumLev='ore'; FromAreaName='Other Flows - Midwest'; end; when('59005') do; FromSumLev='ore'; FromAreaName='Other Flows - South'; end; when('59007') do; FromSumLev='ore'; FromAreaName='Other Flows - West'; end; when('57009') do; FromSumLev='off'; FromAreaName='Other Flows - Foreign'; end; *--redundant?? - same as 98000---; otherwise do; FromSumLev='050'; if fromstate ne tostate then FromAreaName=trim(FromAreaName)||', '||put(fromstate,$fipstab2.); end; end; if fromsumlev ne '050' then fromstab=' '; ***if _n_ le 10 then do; *** list; ***put / _all_; ***end; run; proc contents data=irsmig.usinmig&years. position ; run; proc freq data=irsmig.usinmig&years.(keep=tostate); title2 "State Freq for irsmig.usinmig&years.: INmigration Per Tax Year 2000 Returns"; run; *----Now convert the outmigration file to create the outmig dataset------------------------------------; data irsmig.usoutmig&years.(label="Migrations OUT of all US counties based on returns for tax years &years" ); infile csvinout dsd %if &years ge 0708 %then %str( firstobs=2 ); missover ; ** stopover; ** firstobs=9 ; retain Years "&years"; label years='Tax years on which moves are based'; length FromAreaName $64 FromCounty ToAreaName $64 ToCounty $5 FromSumlev ToSumLev $3 FromState $2 FromCnty $3 ToState $2 ToCnty $3 ToStab $2 _namein $64 NReturns NExemptions 4 &numvars &lennumvars. ; label NReturns='# Tax Returns' NExemptions='# Exemptions on Tax Returns' ; %if &pre1011 %then %do; label TotalIncome='Aggregate Adjusted Gross Income ($000)' MedianIncome='Median Gross Income per Return' AvgHHIncome='Average Gross Income Per Return' PerCapitaIncome='Averge Gross Income Per Exemption'; *<--maybe a misleading name----; %end; %else %do; label Aggr_AGI='Aggregate Adjusted Gross Income ($000)' AvgAGI='Avg AGI'; %end; %if &pre1011 %then %str( informat Nreturns--&lastvarin comma.; ); %if &years ge %str(0506) %then %do; input FromState -- &lastvarin ; **drop MedianIncome; if ToState ne ' '; %end; %else %do; input FromState -- &lastvarin ; *--???---; %end; %addlzs(ToState) %addlzs(ToCnty) %addlzs(FromState) %addlzs(FromCnty) ToAreaName=_namein; drop _: ; length FromStab $2; if FromState > '00' then FromStab=put(FromState,$fipstab2.); %if &pre1011 %then %do; AvgHHIncome=round(TotalIncome*1000/Nreturns); PerCapitaIncome=round(TotalIncome*1000/NExemptions); format NReturns Nexemptions comma9. TotalIncome comma11. MedianIncome--PerCapitaIncome dollar8.; %end; %else %do; if Nreturns then AvgAGI=Aggr_AGI*1000/Nreturns; format AvgAGI dollar7. Aggr_AGI comma11.; %end; FromCounty=FromState||FromCounty; if fromcnty='000' then select(fromstate); when('00') do; FromSumLev='010'; FromAreaName='United States'; end; otherwise do; FromAreaName=put(FromState,$state.); FromSumLev='040'; end; end; else do; FromCounty=FromState||FromCnty; FromAreaName=put(FromCounty,$county.); FromSumlev='050'; end; /*<=========================Comment out===================================== if Fromcnty='000' then do; FromAreaName=put(FromState,$state.); FromSumLev='040'; end; else do; FromCounty=FromState||FromCnty; FromAreaName=put(FromCounty,$county.); FromSumlev='050'; end; ===========================End commented out================================ */ ToCounty=ToState||ToCnty; Select (ToCounty); when('96000') do; ToSumlev='tot'; ToAreaName='Total Mig - US & Foreign'; end; when('97000') do; ToSumlev='tus'; ToAreaName='Total Mig - US'; end; when('97001') do; ToSumlev='tss'; ToAreaName='Total Mig - Same State'; end; when('97003') do; ToSumlev='tds'; ToAreaName='Total Mig - Different State'; end; when('98000') do; ToSumlev='tfo'; ToAreaName='Total Mig - Foreign'; end; when(FromCounty) do; ToSumlev='050'; ToAreaName='Non-Migrants'; end; when('57005') do; ToSumlev='apo'; ToAreaName='Foregign - APO/FPO ZIPS'; end; when('58000') do; ToSumlev='oss'; ToAreaName='Other Flows - Same State'; end; when('59000') do; ToSumLev='ods'; ToAreaName='Other Flows - Different State'; end; when('59001') do; ToSumLev='ore'; ToAreaName='Other Flows - Northeast'; end; when('59003') do; ToSumLev='ore'; ToAreaName='Other Flows - Midwest'; end; when('59005') do; ToSumLev='ore'; ToAreaName='Other Flows - South'; end; when('59007') do; ToSumLev='ore'; ToAreaName='Other Flows - West'; end; when('57009') do; ToSumLev='off'; ToAreaName='Other Flows - Foreign'; end; *--redundant?? - same as 98000---; otherwise do; ToSumLev='050'; if Tostate ne Fromstate then ToAreaName=trim(ToAreaName)||', '||put(Tostate,$fipstab2.); end; end; if ToSumlev ne '050' then Tostab=' '; run; proc freq data=irsmig.usoutmig&years.(keep=fromstate); title2 "State Freq for irsmig.usoutmig&years.: OUTmigration Per Tax Year 2000 Returns"; run; filename csvin clear; filename csvinout clear; *----Now create the Missouri subsets of the us datasets-----; data irsmig.moinmig&years(label="Missouri subset of usinmig&years"); set irsmig.usinmig&years; where ToState='29'; run; data irsmig.mooutmig&years(label="Missouri subset of usoutmig&years"); set irsmig.usoutmig&years; where FromState='29'; run; %endmac: %mend cnvtfls; option mprint; /* =======================Begin comment================ %cnvtfls(9900); options NOmprint; %cnvtfls(0001); %cnvtfls(0102); %cnvtfls(0203); %cnvtfls(0304); *<---rerun this conversion after fix to Ohio and Texas data--; %cnvtfls(0405); %***cnvtfls(0506); *=====================Special 1-shot fix to usoutmig0304. We had 2 copies of the Florida data there so we remove 1 set==; data irsmig.usoutmig0304e(label='Edited version with dup records removed'); set irsmig.usoutmig0304(obs=105328); by fromstate ; if first.fromstate and fromstate='12' then _nfla=0; if fromstate='12' then do; _nfla+1; if _nfla > 5856 then delete; end; drop _:; run; proc freq data=irsmig.usoutmig0304; table fromstate; run; proc freq data=irsmig.usoutmig0304e; table fromstate; run; %cnvtfls(0607); %cnvtfls(0708); %cnvtfls(0809); %cnvtfls(0910); ======================End comment================== */ %cnvtfls(1011);