# Use SPM data to get post tax income for 2010-2014 and 2015-2019

# Make output directory
dir.create('intermediate_data/spm_acs/')


# Loop through 5 years 2015-2019
spm_2015_2019 = NULL

for(yr in 2015:2019){
  spm = read.dta13(paste0('raw_data/spm_acs/spm_',yr,'_pu.dta'))
  
  spm = spm %>% rename(
    spm_wt = wt,
    spm_sex = Sex,
    spm_age = Age,
    spm_mar = Mar,
    spm_edu = Education,
    spm_race = Race,
    spm_hisp = Hispanic,
    spm_puma = PUMA
    
  )
  spm$MULTYEAR = yr
  spm$CBSERIAL = spm$serialno + yr * 10^9
  
  spm_2015_2019 = bind_rows(spm_2015_2019,spm)
}

# Get disposable income - SPM takes out childcare, work expenses, and medical expenses, add those back in
spm_2015_2019 = spm_2015_2019 %>% mutate(
  dispinc = SPM_Resources + SPM_CapWkCCXpns + SPM_MedXpns,
  posttax = SPM_Totval - SPM_FICA - SPM_FedTax - SPM_StTax
)


save(spm_2015_2019, file = 'intermediate_data/spm_acs/spm_2015_2019.Rdata')


# Loop through 5 years 2010-2014
spm_2010_2014 = NULL

for(yr in 2010:2014){
  spm = read.dta13(paste0('raw_data/spm_acs/spm_',yr,'_pu.dta'))
  
  spm = spm %>% rename(
    spm_wt = wt,
    spm_sex = Sex,
    spm_age = Age,
    spm_mar = Mar,
    spm_edu = Education,
    spm_race = Race,
    spm_hisp = Hispanic,
    spm_puma = PUMA
    
  )
  spm$MULTYEAR = yr
  spm$CBSERIAL = spm$serialno + yr * 10^9
  
  spm_2010_2014 = bind_rows(spm_2010_2014,spm)
}

# Get disposable income - SPM takes out childcare, work expenses, and medical expenses, add those back in
spm_2010_2014 = spm_2010_2014 %>% mutate(
  dispinc = SPM_Resources + SPM_CapWkCCXpns + SPM_MedXpns,
  posttax = SPM_Totval - SPM_FICA - SPM_FedTax - SPM_StTax
)
save(spm_2010_2014, file = 'intermediate_data/spm_acs/spm_2010_2014.Rdata')

