x cd /pub/data/irsmig/Tools; %let pgm=cnvt_irsmig_tx9900; filename pgm "&pgm..sas"; *---Read the IRS migration files for 1999-2000 for Texas only and update our US files using this corrected data. John Blodgett, July, 2015. ---; title "&pgm"; libname irsmig '/pub/data/irsmig'; missing d; %macro addlzs(var); &var=translate(right(&var),'0',' '); %mend addlzs; %let years=9900; %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.----; *---when we downloaded the currently available data from irs site it only had aggregate AGI. So we use the old files instead-; filename csvin "../co&years.USi.csv"; filename csvinout "../co&years.USo.csv"; %let pre1011=1; %let numvars=TotalIncome MedianIncome AvgHHIncome PerCapitaIncome ; %let lennumvars=4; %let lastvarin=PerCapitaIncome; data irsmig.txinmig&years(label="Migrations INTO TX counties based on returns for tax years &years"); infile csvin dsd firstobs=119223 missover; *** termstr=crlf; %****str(termstr=crlf); retain Years "&years"; label years='Tax years on which moves are based'; length ToAreaName $64 ToCounty $5 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.; ); input ToState -- MedianIncome; 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.txinmig&years. position ; run; proc freq data=irsmig.txinmig&years.(keep=tostate); title2 "State Freq for irsmig.txinmig&years.: INmigration Per Tax Year 2000 Returns"; run; *----Now convert the outmigration file to create the outmig dataset------------------------------------; data irsmig.txoutmig&years.(label="Migrations OUT of all US counties based on returns for tax years &years" ); infile csvinout dsd firstobs=118815 ;**termstr=crlf missover ; ** stopover; ** firstobs=9 ; retain Years "&years"; label years='Tax years on which moves are based'; length FromAreaName $64 FromCounty $5 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; %if &pre1011 %then %str( informat Nreturns--&lastvarin comma.; ); input FromState -- &lastvarin ; %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; 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.txoutmig&years.(keep=fromstate); title2 "State Freq for irsmig.txoutmig&years.: OUTmigration Per Tax Year 2000 Returns"; run; filename csvin clear; filename csvinout clear; %endmac: %mend cnvtfls; option mprint; %cnvtfls(9900); *----------------------Now insert this updated TX data into the US data sets--------------------------------*; data irsmig.usinmig9900e; set irsmig.usinmig9900(in=inold where=(ToState ne '48')) irsmig.txinmig9900(in=innew); by Tostate; **if tostate='48' and inold then delete; *--we throw away the old TX obs but keep the new---; run; data irsmig.usoutmig9900e; set irsmig.usoutmig9900(in=inold where=(FromState ne '48')) irsmig.txoutmig9900(in=innew); by Fromstate; * if Fromstate='48' and inold then delete; *--we throw away the old TX obs but keep the new---; run; *---the following was run from DM after verifying that prior steps were creating useful output---; proc datasets library=irsmig memtype=data; change usinmig9900=usinmig9900_old; change usinmig9900e=usinmig9900; change usoutmig9900=usoutmig9900_old; change usoutmig9900e=usoutmig9900; quit; run;