program define taxsimlocal27
version 9.0
/*

net from http://www.nber.org/stata
net describe taxsimlocal27
net install taxsimlocal27

*/
display "begin taxsimlocal27.ado on $S_DATE (version 27.1)"
syntax [, Full Secondary Wages Interest Long Output(string) Debug PLAN(integer 0) PLANVal(real 0) Replace]

tempfile original
if `"`debug'"' != `"debug"' {
     tempfile outfile infile ftp msg serverfile
}
else {
     display "Saving input to FORTRAN TAXSIM as txpydata.raw "
     local outfile txpydata.raw
     display "Saving output from FORTRAN TAXSIM as results.raw "
     local infile results.raw
     capture erase txpydata.raw
     capture erase results.raw
}
capture { rm `outfile' }
capture { rm `infile'  }
capture { rm ftp.txt   }
capture ( rm msg.txt   }

if length(`"`output'"') == 0 {
     local output taxsim_out
}
display "TAXSIM results will be saved in `output'.dta"

**  mtr is marginal is tax rate.
local mtr 85
**  idtl is output. 0 is basic
local idtl 0

#delimit ;
**  marginal tax rate options ;
if `"`secondary'"' == `"secondary"' { ;
     local mtr 86 ;
     display "Marginal rate with respect to secondary earner";
} ;
if `"`wages'"' == `"wages"' { ;
     local mtr 11 ;
     display "Marginal rate with respect to overall earnings";
} ;
if `"`interest'"' == `"interest"' { ;
     local mtr 14 ;
     display "Marginal rate with respect to interest income";
} ;
if `"`long'"' == `"long"' { ;
     local mtr 70 ;
     display "Marginal rate with respect to long term gains";
} ;

if `"`full'"' == `"full"' { ;
     local idtl 2 ;
     local addvars v10 v11 v12 v13 v14 v15 v16 v17 v18 v19
                        v20 v21 v22 v23 v24 v25 v26 v27 v28 v29
                        v30 v31 v32 v33 v34 v35 v36 v37 v38 v39
                        v40 v41 ;
     display "Full Intermediate Calculations Requested";
} ;
if "`mtr'" == "85" { ;
     display "Marginal rate with respect to primary earner";
} ;
if "`idtl'" == "0" { ;
     display "Basic output";
} ;
/*
if `plan' != 0 {;
   display "Tax law modified according to TAXSIM plan " `plan';
};
if `planval' != 0 {;
   display "Modified tax parameter is " `planval';
};
*/
#delimit cr
** Checking data sent to taxsim for validity
** Checking for observations prior to variables for better error message.
if _N==0 {
  display "Aborting:  No observations in data set"
  exit
}

#delimit ;
local invars year state mstat page sage depx dep13 dep17 dep18
      pwages swages dividends intrec stcg ltcg otherprop
      nonprop pensions gssi ui transfers rentpaid proptax
      otheritem childcare mortgage;
#delimit cr

local not_found "are OK."

di "Variables:"
foreach X of local invars {
     capture confirm var `X'
     if _rc > 0 {
          display "`X' " _continue
          local not_found  are not found, and are set to zero.
          generate `X' = 0
     }
}
di _newline "`not_found'"

foreach X of local invars {
     capture confirm numeric variable `X'
     if _rc > 0 {
          display "Aborting:  `X' is not numeric.  See help tostring."
          exit
     }
     capture assert `X'!=.
     if _rc > 0 {
          display "Aborting:  `X' contains missing value codes."
          exit
     }
}

if `"`debug'"' == `"debug"' {
   display "Here are the variable counts and means:"
   summarize `invars'
}

di "`mtr' `idtl' `plan' `planval'"
capture drop taxsim_id
generate taxsim_id = _n
save `original',replace
tostring taxsim_id,replace
capture {
  replace taxsim_id = "-1" if taxsimid==-1
  replace taxsim_id = "-2" if taxsimid==-2
}
quietly replace taxsim_id ="27 `mtr' `idtl' `plan' `planval' /" + char(10) + taxsim_id in 1
quietly summarize mstat
local obs=r(N)
di "`obs' obs"
outfile taxsim_id `invars' using `outfile',noquote nolabel
di "`c(os)'"

   ! taxsim27 <`outfile' >`infile'
   di "using local taxsim27"

local results taxsim_id year state fiitax siitax fica frate srate ficar `addvars'
quietly import delimited using "`infile'",clear varnames(1) delimiter(" ",collapse)

summarize

di "`obs' obs"
quietly describe
if ( `r(N)'==0 ) {
   display "No calculations were returned by the tax calculator."
   display "For taxsimlocal27, this probably means the taxcalc27.exe file"
   display "was not in the executable path. For taxsim27 this is probably"
   display "a firewall problem. See:
   display "  http://www.nber.org/taxsim/ftp-problems.html"
   exit
}
if ( `r(N)'-`obs'!=0 & `r(N)'- 51*`obs'!=0 ) {
   display "An error occurred. There should be a report in this log.
   display "Consult the Notes and Support section of the help file for troubleshooting info"
   display "http://www.nber.org/taxsim/stata/ "
   display "`obs' records sent and `r(N)' records received"
   exit
}
label variable state  "state id"
label variable fiitax "Federal Income Tax"
label variable siitax "State Income Tax"
label variable fica   "OASDI and HI Payroll Tax"
label variable frate  "IIT marginal rate"
label variable srate  "state marginal rate"
label variable ficar  "SS marginal rate"
if `"`full'"' == `"full"' {
label variable v10 "Federal AGI"
label variable v11 "UI in AGI"
label variable v12 "Social Security in AGI"
label variable v13 "Zero Bracket Amount"
label variable v14 "Personal Exemptions"
label variable v15 "Exemption Phaseout"
label variable v16 "Deduction Phaseout"
label variable v17 "Deductions allowed"
label variable v18 "Federal Taxable Income"
label variable v19 "Federal Regular Tax"
label variable v20 "Exemption Surtax"
label variable v21 "General Tax Credit"
label variable v22 "Child Tax Credit (as adjusted)"
label variable v23 "Refundable Part of Child Tax Credit"
label variable v24 "Child Care Credit"
label variable v25 "Earned Income Credit"
label variable v26 "Income for the Alternative Minimum Tax"
label variable v27 "AMT Liability (addition to regular tax)"
label variable v28 "Income Tax before Credits"
label variable v29 "FICA"
label variable v30 "State Household Income"
label variable v31 "State Rent Payments"
label variable v32 "State AGI"
label variable v33 "State Exemption amount"
label variable v34 "State Standard Deduction"
label variable v35 "State Itemized Deductions"
label variable v36 "State Taxable Income"
label variable v37 "State Property Tax Credit"
label variable v38 "State Child Care Credit"
label variable v39 "State EITC "
label variable v40 "State Total Credits"
label variable v41 "State Bracket Rate"
}
capture save `output', replace
capture drop _merge
capture merge 1:m taxsim_id state year using `original'
if _rc != 0 {
  di "Merge failed"
  tab _merge
}

capture drop taxsim_id _merge
end