**********************************************************************************
* Code to replicate the results presented in Attanasio and Rohwedder (2003):     *
*  Pension Wealth and Household Saving: Evidence from Pension Reforms in the UK  *
*  American Economic Review, December 2003.                                      *
**********************************************************************************

drop _all
cap log c

set mem 180000
set matsize 1500
set more on

u AER_Attanasio_Rohwedder_data


gen  int publ  = 1000      /* neither hoh nor spouse in public scheme, i.e. no ci==1 */
replace publ   = 2000 if ((cistat1==1 & cistat2==0)|(cistat1==0&cistat2==1))        /* one of them is in publ scheme, i.e. one ci==1 */
replace publ   = 3000 if ((cistat1==1 & cistat2==1)|(cistat1==1&cistat2==.))         /* hoh is contracted-in, and if spouse present then her as well  */

drop cistat1 cistat2

gen int cp  = coh1*10 + publ
gen int co  = coh1*10 + occ1
gen int cop = coh1*10 + occ1 + publ


* Define Estimation Sample: 
* only HH where head of household has not reached public pension age
drop if age1>65

*********     trimming of the means w.r.t. total disp. HH income     **************
egen p2inc  = pctile(bhcinc), by(year) p(2)
egen p98inc  = pctile(bhcinc), by(year) p(98)
drop if bhcinc<p2inc
drop if bhcinc>p98inc
*******************************************
* save interim data set to make it accessible for the subsequent different tasks
save tempdat, replace

sort coh1 year
drop if publ==1000
collapse netbpsp3 occ1 age1 netbp3 netsp3 totexp,by(coh1 year)

reshape wide occ1 age1 netbpsp3 netbp3 netsp3 totexp, i(coh1) j(year)

*75
gen dtotexp75    = totexp75   - totexp74	if totexp75    !=. & totexp74   !=.
gen dtotpw375    = netbpsp375 - netbpsp374	if netbpsp375  !=. & netbpsp374 !=.
gen dnetbp375    = netbp375   - netbp374	if netbp375    !=. & netbp374   !=.
gen dnetsp375    = netsp375   - netsp374	if netsp375    !=. & netsp374   !=.

*76
gen dtotexp76    = totexp76   - totexp75        if totexp76    !=. & totexp75   !=.
gen dtotpw376    = netbpsp376 - netbpsp375	if netbpsp376  !=. & netbpsp375 !=.
gen dnetbp376    = netbp376   - netbp375	if netbp376    !=. & netbp375   !=.
gen dnetsp376    = netsp376   - netsp375	if netsp376    !=. & netsp375   !=.

*77
gen dtotexp77    = totexp77   - totexp76	if totexp77    !=. & totexp76   !=.
gen dtotpw377    = netbpsp377 - netbpsp376	if netbpsp377  !=. & netbpsp376 !=.
gen dnetbp377    = netbp377   - netbp376	if netbp377    !=. & netbp376   !=.
gen dnetsp377    = netsp377   - netsp376	if netsp377    !=. & netsp376   !=.

*78
gen dtotexp78    = totexp78   - totexp77	if totexp78    !=. & totexp77   !=.
gen dtotpw378    = netbpsp378 - netbpsp377	if netbpsp378  !=. & netbpsp377 !=.
gen dnetbp378    = netbp378   - netbp377	if netbp378    !=. & netbp377   !=.
gen dnetsp378    = netsp378   - netsp377	if netsp378    !=. & netsp377   !=.

*79
gen dtotexp79    = totexp79   - totexp78	if totexp79    !=. & totexp78   !=.
gen dtotpw379    = netbpsp379 - netbpsp378	if netbpsp379  !=. & netbpsp378 !=.
gen dnetbp379    = netbp379   - netbp378	if netbp379    !=. & netbp378   !=.
gen dnetsp379    = netsp379   - netsp378	if netsp379    !=. & netsp378   !=.

*80
gen dtotexp80    = totexp80   - totexp79	if totexp80    !=. & totexp79   !=.
gen dtotpw380    = netbpsp380 - netbpsp379	if netbpsp380  !=. & netbpsp379 !=.
gen dnetbp380    = netbp380   - netbp379	if netbp380    !=. & netbp379   !=.
gen dnetsp380    = netsp380   - netsp379	if netsp380    !=. & netsp379   !=.

*81
gen dtotexp81    = totexp81   - totexp80	if totexp81    !=. & totexp80   !=.
gen dtotpw381    = netbpsp381 - netbpsp380	if netbpsp381  !=. & netbpsp380 !=.
gen dnetbp381    = netbp381   - netbp380	if netbp381    !=. & netbp380   !=.
gen dnetsp381    = netsp381   - netsp380	if netsp381    !=. & netsp380   !=.

*82
gen dtotexp82    = totexp82   - totexp81	if totexp82    !=. & totexp81   !=.
gen dtotpw382    = netbpsp382 - netbpsp381	if netbpsp382  !=. & netbpsp381 !=.
gen dnetbp382    = netbp382   - netbp381	if netbp382    !=. & netbp381   !=.
gen dnetsp382    = netsp382   - netsp381	if netsp382    !=. & netsp381   !=.

*83
gen dtotexp83    = totexp83   - totexp82	if totexp83    !=. & totexp82   !=.
gen dtotpw383    = netbpsp383 - netbpsp382	if netbpsp383  !=. & netbpsp382 !=.
gen dnetbp383    = netbp383   - netbp382	if netbp383    !=. & netbp382   !=.
gen dnetsp383    = netsp383   - netsp382	if netsp383    !=. & netsp382   !=.

*84
gen dtotexp84    = totexp84   - totexp83	if totexp84    !=. & totexp83   !=.
gen dtotpw384    = netbpsp384 - netbpsp383	if netbpsp384  !=. & netbpsp383 !=.
gen dnetbp384    = netbp384   - netbp383	if netbp384    !=. & netbp383   !=.
gen dnetsp384    = netsp384   - netsp383	if netsp384    !=. & netsp383   !=.

*85
gen dtotexp85    = totexp85   - totexp84	if totexp85    !=. & totexp84   !=.
gen dtotpw385    = netbpsp385 - netbpsp384	if netbpsp385  !=. & netbpsp384 !=.
gen dnetbp385    = netbp385   - netbp384	if netbp385    !=. & netbp384   !=.
gen dnetsp385    = netsp385   - netsp384	if netsp385    !=. & netsp384   !=.

*86
gen dtotexp86    = totexp86   - totexp85	if totexp86    !=. & totexp85   !=.
gen dtotpw386    = netbpsp386 - netbpsp385	if netbpsp386  !=. & netbpsp385 !=.
gen dnetbp386    = netbp386   - netbp385	if netbp386    !=. & netbp385   !=.
gen dnetsp386    = netsp386   - netsp385	if netsp386    !=. & netsp385   !=.

*87
gen dtotexp87    = totexp87   - totexp86	if totexp87    !=. & totexp86   !=.
gen dtotpw387    = netbpsp387 - netbpsp386	if netbpsp387  !=. & netbpsp386 !=.
gen dnetbp387    = netbp387   - netbp386	if netbp387    !=. & netbp386   !=.
gen dnetsp387    = netsp387   - netsp386	if netsp387    !=. & netsp386   !=.

*****************************************************************************************************
* generate the %age changes betw. the all the years, not just reform yrs(74/75; 77/78; 80/81; 85/86)*
*****************************************************************************************************
*** 75 ****
gen pctd_totexp75     = dtotexp75 /totexp74
gen pctd_totpw375     = dtotpw375 /netbpsp374
gen pctd_netbp375     = dnetbp375 /netbp374
gen pctd_netsp375     = dnetsp375 /netsp374

*** 76 ****
gen pctd_totexp76     = dtotexp76 /totexp75
gen pctd_totpw376     = dtotpw376 /netbpsp375
gen pctd_netbp376     = dnetbp376 /netbp375
gen pctd_netsp376     = dnetsp376 /netsp375

**** 77 ****
gen pctd_totexp77     = dtotexp77 /totexp76
gen pctd_totpw377     = dtotpw377 /netbpsp376
gen pctd_netbp377     = dnetbp377 /netbp376
gen pctd_netsp377     = dnetsp377 /netsp376

***** 78  ********
gen pctd_totexp78     = dtotexp78 /totexp77
gen pctd_totpw378     = dtotpw378 /netbpsp377
gen pctd_netbp378     = dnetbp378 /netbp377
gen pctd_netsp378     = dnetsp378 /netsp377

***** 79 *********
gen pctd_totexp79     = dtotexp79 /totexp78
gen pctd_totpw379     = dtotpw379 /netbpsp378
gen pctd_netbp379     = dnetbp379 /netbp378
gen pctd_netsp379     = dnetsp379 /netsp378

******* 80 *********
gen pctd_totexp80     = dtotexp80 /totexp79
gen pctd_totpw380     = dtotpw380 /netbpsp379
gen pctd_netbp380     = dnetbp380 /netbp379
gen pctd_netsp380     = dnetsp380 /netsp379

******* 81 *********
gen pctd_totexp81     = dtotexp81 /totexp80
gen pctd_totpw381     = dtotpw381 /netbpsp380
gen pctd_netbp381     = dnetbp381 /netbp380
gen pctd_netsp381     = dnetsp381 /netsp380

******* 82 *********
gen pctd_totexp82     = dtotexp82 /totexp81
gen pctd_totpw382     = dtotpw382 /netbpsp381
gen pctd_netbp382     = dnetbp382 /netbp381
gen pctd_netsp382     = dnetsp382 /netsp381

******* 83 *********
gen pctd_totexp83     = dtotexp83 /totexp82
gen pctd_totpw383     = dtotpw383 /netbpsp382
gen pctd_netbp383     = dnetbp383 /netbp382
gen pctd_netsp383     = dnetsp383 /netsp382

******* 84 *********
gen pctd_totexp84     = dtotexp84 /totexp83
gen pctd_totpw384     = dtotpw384 /netbpsp383
gen pctd_netbp384     = dnetbp384 /netbp383
gen pctd_netsp384     = dnetsp384 /netsp383

******* 85 *********
gen pctd_totexp85     = dtotexp85 /totexp84
gen pctd_totpw385     = dtotpw385 /netbpsp384
gen pctd_netbp385     = dnetbp385 /netbp384
gen pctd_netsp385     = dnetsp385 /netsp384

******* 86 *********
gen pctd_totexp86     = dtotexp86 /totexp85
gen pctd_totpw386     = dtotpw386 /netbpsp385
gen pctd_netbp386     = dnetbp386 /netbp385
gen pctd_netsp386     = dnetsp386 /netsp385

******* 87 *******
gen pctd_totexp87     = dtotexp87 /totexp86
gen pctd_totpw387     = dtotpw387 /netbpsp386
gen pctd_netbp387     = dnetbp387 /netbp386
gen pctd_netsp387     = dnetsp387 /netsp386


reshape long age1 dtotexp dtotpw3 dnetsp3 dnetbp3 netbpsp3 netsp3 netbp3 totexp  /*
*/           occ1 pctd_totexp pctd_totpw3 pctd_netbp3 pctd_netsp3, i(coh1) j(year) 

gen percSERPS=netsp3/netbpsp3

log using Attanasio_Rohwedder_results,replace t

* Table 1: Expected Pension Wealth from BSP (amounts in 1996 British pounds)
table year coh1 if coh1<61 &(year==74|year==75|year==80|year==81), c(mean netbp3 mean pctd_netbp3)

* Table 2: EPW from SERPS by cohort: PDV in 1996 British Pounds, Percent of total EPW in 1975
table year coh1 if coh1<61 & year==75, c(mean netsp3 mean percSERPS)

log c

* Figure 1: Cohort-age profiles for pension wealth from BSP, 1974-1987
lab var age1 "age of the head of household"
gr7 netbp3 age1 if (coh1==11|coh1==26|coh1==36|coh1==46|coh1==56), yla(0 (20000) 100000) xla(20 (4) 64) c(LLLLLLL) l2(EPW from BSP, [1996 GBP]) l1(" ") s(.) saving(AER_AR03_Fig1,replace)

*******************  Data Work for Table 3 (Data need to be aggregated to a different group level)  ****************
drop _all
u tempdat

* merge two occupation groups to obtain larger cellsizes for desriptive statistics
replace occ1=3 if occ1==2

collapse netbpsp3 age1 netbp3 netsp3 totexp,by(coh1 publ occ1 year)

gen percSERPS=netsp3/netbpsp3

log using Attanasio_Rohwedder_results,append t
* Table 3: EPW from SERPS expressed as a percentage of total Public Pension Wealth by cohort and occupation in 1975
table occ1 coh1 if coh1<61 & publ==2000 &year==75, c(mean percSERPS)  /* Couples one in, one out    */
table occ1 coh1 if coh1<61 & publ==3000 &year==75, c(mean percSERPS)  /* Couples both in, single in */
log c


*** Figure 2: Cohort-age profiles for pension wealth from SERPS, 1974-1987
drop _all  /* again different aggregation level required */
u tempdat
*sort coh1 publ year
collapse netbpsp3 age1 netbp3 netsp3 totexp,by(coh1 publ year)
lab var age1 "age of the head of household"

**  Figure 2(A): Couples with one adult opted out, one in
gr7 netsp3 age1 if publ==2000 & (coh1==11|coh1==21|coh1==31|coh1==41|coh1==56), yla(0 (5000) 30000) xla(20 (4) 64) c(LLLLL) l2(EPW from SERPS, [1996 GBP]) l1(" ") s(.) saving(AER_AR03_Fig2a,replace)
**  Figure 2(B): Couples both in SERPS, singles in SERPS
gr7 netsp3 age1 if publ==3000 & (coh1==11|coh1==21|coh1==31|coh1==41|coh1==56), yla(0 (5000) 30000) xla(20 (4) 64) c(LLLLL) l2(EPW from SERPS, [1996 GBP]) l1(" ") s(.) saving(AER_AR03_Fig2b,replace)


**************** Data Work for Table 4 (uses the micro data) **************************
drop _all
u tempdat

* exclude those we do not include in regressions, i.e. occ-group 5
drop if occ1==5

* gen year and group dummies
qui tab year, gen(yr)
qui tab cop, gen (cop)

replace netbpsp3=netbpsp3/1000
replace totexp=totexp/1000

* purge totpw3 totexp and sr_bhc of year and group effects
qui reg netbpsp yr1-yr14 cop1-cop144,noc
predict Rtotpw3, re

qui reg totexp yr1-yr14 cop1-cop144,noc
predict Rtotexp, re

qui reg sr_bhc yr1-yr14  cop1-cop144, noc
predict Rsr_bhc, re

log using Attanasio_Rohwedder_results, append text

* Table 4: Regression of Saving Rates and Consumption on EPW, conditioning on group and time effects
* consumption and EPW in thousand pounds

reg Rsr_bhc Rtotpw3
reg Rtotexp Rtotpw3

log c


**  Table 5: Regression Estimates are produced in SAS (due to matrix size limitations
**           in STATA at the time of performing the data work)
**           see SAS program "AER_AR03_Table5.sas"
**           Generation of input data for that program is documented at the end 
**           of this program
******************************************************************************
* Using estimates produced in SAS (displayed in Table 5) - generate Figure 3 *
******************************************************************************

clear _all
cap log c

set obs 45
gen age=_n+18

gen     bsp_est  =-0.3061  if age>=20 & age<=31
replace bsp_est  = 0.0060  if age>=32 & age<=42
replace bsp_est  = 0.0432  if age>=43 & age<=53
replace bsp_est  = 0.0351  if age>=54 & age<=64

gen     serps_est= 0.0135  if age>=20 & age<=31
replace serps_est=-0.5472  if age>=32 & age<=42
replace serps_est=-0.6511  if age>=43 & age<=53 
replace serps_est=-0.7487  if age>=54 & age<=64

lab var bsp_est "response BSP"
lab var serps_est "response SERPS"

gr7 bsp_est serps_est age, xla(20 (5) 65) yla ytick(-0.8 (0.1) 0) c(ll) s(.d) saving(AER_AR03_Fig3,replace)

***********************************************************************************
****  Data preparation for SAS data set on which IV-estimation is performed *******
****  Estimation itself is carried out in SAS (AER_AR03_Table5.sas)         *******
****  -- data generation currently commented out, because we included the   *******
****     resulting data set and converted it into SAS so that it can be     *******
****     used with the SAS program without further processing.              *******
***********************************************************************************
/*  
clear _all
u tempdat

gen r       = 0.03         /* real interest rate*/
gen elsubst = 1
gen beta    = 0.98
gen d 	= beta^elsubst * ((1+r)^(elsubst-1))

gen T       = 55           /* number of LC-periods, period 1 at age 20 */
gen t       = age1-20+1    /* number current period if first period = age 20 */
gen t_sp    = 75 -dob1 - 20 + 1    /* number of LC-period of SERPS reform w.r.t. ind's lifespan */
gen t_bp    = 80 -dob1 - 20 + 1    /* number of LC-period of BSP   reform w.r.t. ind's lifespan */


gen     adj = ((1-beta)* beta^(t-1))/(1-(beta^T))       
replace adj = ((1-beta)* beta^(t-t_sp-1))/(1-(beta^(T-t_sp)))  if year > 75 & year <=80 & t_sp>0 
replace adj = ((1-beta)* beta^(t-t_bp-1))/(1-(beta^(T-t_bp)))  if year > 80 & t_bp > 0


* net PW variables divided by total HH income: bhcinc
gen	netsp3r_bhc          = netsp3/bhcinc
gen	netbp3r_bhc          = netbp3/bhcinc
gen	netbpsp3r_bhc        = netbpsp3/bhcinc

* apply adjustment factor so that coefficients can be directly interpreted as elasticities
replace netsp3r_bhc       = netsp3r_bhc    	* adj
replace netbp3r_bhc       = netbp3r_bhc    	* adj


* renaming to have shorter names in SAS
ren netbp3r_bhc nb3r
ren netsp3r_bhc ns3r


compress
sort cop year
replace occ1=2 if occ1==3  /* merge two occupation groups due to obtain larger cellsize */
replace cop=coh1*10 + occ1 + publ

qui tab year, gen(y) 
qui tab cop,  gen(c) 


cap prog drop interact

                
prog def interact

local count_d=1
local count_y=1
gen   byte d1     =0

while `count_y' < 15 { 
      local count_c=1
      while `count_c' < 132 {

		gen ind_action=0
 		qui replace ind_action=1 if y`count_y'==1 & c`count_c'==1  
		qui replace d`count_d'=1 if y`count_y'==1 & c`count_c'==1  
								
		egen realaction = sum(ind_action)
		if realaction > 0 & realaction!=.{
						    local count_d = `count_d' + 1 
						    gen byte d`count_d'=0  
						 }
		drop realaction
		drop ind_action
		compress
		local count_c = `count_c' + 1		
   		            }

local count_y = `count_y' + 1
}

end

interact

compress
* save alpreadj,replace
*/
