program define taxpuf27
version 8.3

** by Jean Roth , jroth@nber.org

display "begin taxpuf27.ado version of 6 December 2017"
syntax [, Secondary Wages Interest Long Output(string) Temp]

if `"`temp'"' != `"temp"' {
     tempfile tdata tmpresult
}
else {
     display "Saving input to FORTRAN TAXSIM as tdata.raw "
     local tdata tdata.raw
     cap rm tdata.raw
     display "Saving output from FORTRAN TAXSIM as tmpresult.raw "
     local tmpresult tmpresult.raw
     cap rm tmpresult.raw
}

if length(`"`output'"') == 0 {
     local output taxsim_out
}

preserve

cap replace data252=2
cap generate data252 = 2
cap quietly gen     data253 = 85
cap quietly replace data253 = 85
if `"`secondary'"' == `"secondary"' {
     quietly replace data253 = 86
     display "Marginal rate with respect to secondary earner"
}
if `"`wages'"' == `"wages"' {
     quietly replace data253 = 11
     display "Marginal rate with respect to overall earnings"
}
if `"`interest'"' == `"interest"' {
     quietly replace data253 = 14
     display "Marginal rate with respect to interest income"
}
if `"`long'"' == `"long"' {
     quietly replace data253 = 70
     display "Marginal rate with respect to long term gains"
}
if data253[1] == 85 {
     display "Marginal rate with respect to primary earner"
}

cap generate data255 = _n

** Check data sent to taxsim for validity
** Checking data set for observations
capture assert _N > 0
if _rc > 0 {
  display "Aborting:  No observations in data set"
  exit
}

local vlist " "
forvalues X = 1/255 {
     local vlist `vlist' data`X'
     cap gen     data`X' = 0
     cap replace data`X' = 0 if data`X'==.
}
** Clean data103
*quietly drop if data103<1960 | data103>2013 | data103==.
scalar n103 = _N
scalar nstart = _N
scalar m103 = nstart - _N
if m103 display "Dropping " m103 " observations with out of range data103 values"
** Clean data6
*uietly drop if data6<0 | data6 > 51 | missing(data6)
** The maximum value of data103 will need updating annually ;
*uietly drop if (data103<1977|data103>2013)&missing(data6)!=1&data6!=0
scalar n6 = _N
scalar m6 = n103 - _N
if m6 display "Dropping " m6 " observations with out of range data6 values"
** Clean data8
quietly drop if data8<0 | data8>999
scalar n8 = _N
scalar m8 = n6 - _N
if m8 display "Dropping " m8 " observations with out of range data8 values"
*quietly drop if data2<1 | (data2>3 & data2<7) | data2>7
quietly drop if data2<1 | data2>7
scalar n2 = _N
scalar m2 = n8 - _N
if m2 display "Dropping " m2 " observations with out of range data2 values"
** Clean data9
quietly drop if data9<0 | data9>2
scalar n9 = _N
scalar m9 = n2 - _N
if m9 display "Dropping " m9 " observations with out of range data9 values"
gen id = _n
assert _N==n103
display "Sending " _N " observations to taxpuf "
outfile `vlist'  using `tdata', noquote comma
! /home/nber/taxsim/public_html/taxsim27/taxpuf27-Linux <`tdata' >`tmpresult'
local results data100  data103 state fiitax siitax fica frate srate ficar s1-s12 c1-c156
infile  `results' using  `tmpresult'  ,clear
label variable fiitax "Federal Income Tax"
label variable siitax "Stata 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"
sort data103 data100
save "`output'", replace
restore
display " "
display "TAXSIM results are saved in `output'"
display " "
merge 1:1 data103 data100 using `output',nogenerate update replace
end