?To be run on Limdep
RESET$
?To increase number of variables go to Project/Settings and change Cells number and rows.
?First go to tools/project and change number of cells to at least 93,830,276. Then, do: 
Rows;400000$

?To load the file created in Stata run the following (This may take a while, so be patient)
Read ; Nobs=342868 ; Nvar=18 ; file="C:\Users\DIEGO\Documents\MyDocumentsThink\Koetter Data\replicatestata\sfadata.txt"$
? Save this file for easy loading.
SAVE;file="C:\sfa_data.lpj"$
?To load the file with the initial variables already in LIMDEP use this:
LOAD;file="C:\sfa_data.lpj"$

rename; x1=year$
rename; x2=entity$
rename; x3=TA$
rename; x4=opex$
rename; x5=Y1$
rename; x6=y2$
rename; x7=w1$ 
rename; x8=w2$
rename; x9=w3$
rename; x10=TOC$
rename; x11=REV$
rename; x12=PBT $
rename; x13= Z$
rename; x14=PBTneg$
rename; x15=NPDPBT$
rename; x16=PATneg$
rename; x17=NPDPAT$
rename; x18=idunique$
?The variable idunique assign a unique number to each observation. 

? *****************************************************************
? Creating log and interaction terms
? *****************************************************************

dstat; rhs= year, entity, opex, y1,y2,w1,w2,w3,toc, rev, pbt, z, pbtneg, npdpbt, patneg, npdpat, TA $

create;lnTOC=log(TOC/w3)$
create;lnOPEX=log(OPEX/w3)$
create;lnPBT=log(pbtneg/w3)$
create;lnNPI=log(npdpbt/w3)$
create;lny1=log(y1)$
create;lny2=log(y2)$
create;lnw1=log(w1/w3)$
create;lnw2=log(w2/w3)$
create;lnw3=log(w3/w3)$
create;lnz1=log(z)$
create;ln5y1y1=0.5*lny1*lny1$
create;lny1y2=lny1*lny2$
create;ln5y2y2=0.5*lny2*lny2$
create;ln5w1w1=0.5*lnw1*lnw1$
create;lnw1w2=lnw1*lnw2$
create;ln5w2w2=0.5*lnw2*lnw2$
create;ln5z1=0.5*lnz1*lnz1$
create;lny1w1=lny1*lnw1$
create;lny1w2=lny1*lnw2$
create;lny2w1=lny2*lnw1$
create;lny2w2=lny2*lnw2$
create;lny1z1=lny1*lnz1$
create;lny2z1=lny2*lnz1$
create;lnw1z1=lnw1*lnz1$
create;lnw2z1=lnw2*lnz1$
create;t=year-1975$
create;t2=t*t$
create;lny1t=lny1*t$
create;lny2t=lny2*t$
create;lnw1t=lnw1*t$
create;lnw2t=lnw2*t$
create;lnz1t=lnz1*t$
? *******************************
? Recoding to calculate SE and TC
? *******************************
create;a1=lnw1$
create;a2=lnw2$
create;b1=lny1$
create;b2=lny2$
create;c1=lnz1$
create;a11=ln5w1w1$
create;a12=lnw1w2$
create;a22=ln5w2w2$
create;b11=ln5y1y1$
create;b12=lny1y2$
create;b22=ln5y2y2$
create;c11=ln5z1$
create;d11=lny1w1$
create;d12=lny1w2$
create;d21=lny2w1$
create;d22=lny2w2$
create;e11=lny1z1$
create;e21=lny2z1$
create;f11=lnw1z1$
create;f21=lnw2z1$
create;g1=t$
create;g2=t2$
create;g11=lny1t$
create;g12=lny2t$
create;g21=lnw1t$
create;g22=lnw2t$
create;g31=lnz1t$
delete;ln*$
create;lnTOC=log(toc/w3)$
create;lnPBT=log(pbtneg/w3)$
create;lnNPI=log(npdpbt/w3)$
create;lnOPEX=log(OPEX/w3)$

namelist;SFA=a1,a2,b1,b2,c1,a11,a12,a22,b11,b12,b22,d11,d12,d21,d22,g1,g2,g11,g12,g21,g22$

? **********************
? OLS without efficiency
? Estimate MC_ols as in KKS and MC_olsa using C_ols instead of OPEX.
? **********************
regress;cost;lhs=lnOPEX;rhs=one,SFA;het; keep=PlnTOCls$
create;a3=log(w3)$
calc;b_d13=(-1)*(b_d11+b_d12)$
calc;b_d23=(-1)*(b_d21+b_d22)$
create;SEC_y1=b_b1+b_b11*b1+b_b12*b2+b_d11*a1+b_d12*a2+b_d13*a3+b_g11*g1$ /*This procedure adds b_d13*a3 incorrectly, with a3=log(w3).But, a1=log(w1/w3) 
and a2=log(w2/w3). Thus, the expression already has b_d13*a3 implicit in the homogeneity constraint. The same applies for SEC_y2*/
create;SEC_y2=b_b2+b_b12*b1+b_b22*b2+b_d21*a1+b_d22*a2+b_d23*a3+b_g12*g1$
create;SEC_OLS=SEC_y1+SEC_y2$
create;MC_ols=SEC_OLS * OPEX/TA$
create;C_ols=exp(PlnTOCls)*w3$
create;MC_OLSa=SEC_OLS*C_ols/TA$
dstat;rhs=MC*,SEC*$
dstat; rhs=OPEX, C_ols$
Calc;delete b_d13,b_d23$

? *********************************************************
? OLS without efficiency estimating marginal cost:
? MC_olsb and MC_olsc are higher than MC_ols 
? *********************************************************
regress;cost;lhs=lnOPEX;rhs=one,SFA;het; keep=PlnTOCls$
create;a3=log(w3)$
create;SEC_y1b=b_b1+b_b11*b1+b_b12*b2+b_d11*a1+b_d12*a2+b_g11*g1$
create;SEC_y2b=b_b2+b_b12*b1+b_b22*b2+b_d21*a1+b_d22*a2+b_g12*g1$
create;SEC_OLSb=SEC_y1b+SEC_y2b$
create;MC_olsb=SEC_OLSb * OPEX/TA$
create;C_ols=exp(PlnTOCls)*w3$
create;MC_olsc=SEC_OLSb*C_ols/TA$
dstat;rhs=MC*,SEC*$
dstat; rhs=OPEX, C_ols$


? ****
? COST: Same as KKS
? ****
sample;all$
skip$
create;wts=z/TA$
FRONTIER;cost;lhs=lnOPEX;rhs=one,SFA;Eff=u;tlg=.01;tlb=0.001;tlf=0.001;wts=wts;keep=PlnTOC;res=res; Alg=B$
create;CE=exp(-u)$
create;PTOC=exp(PlnTOC)*w3$
create;ATOC=PTOC/TA$
dstat;rhs=CE; Str=YEAR$


? ***************************
? Calculating scale economies
? ***************************
calc;b_d13=(-1)*(b_d11+b_d12)$
calc;b_d23=(-1)*(b_d21+b_d22)$
create;SEC_y1=b_b1+b_b11*b1+b_b12*b2+b_d11*a1+b_d12*a2+b_d13*a3+b_g11*g1$
create;SEC_y2=b_b2+b_b12*b1+b_b22*b2+b_d21*a1+b_d22*a2+b_d23*a3+b_g12*g1$
create;SEC_y=SEC_y1+SEC_y2$
create;RTS=1/SEC_y$
dstat; rhs=RTS$
Calc;delete b_d13,b_d23$

? *****************************************
? Calculating scale economies. 
? RTSa estimates are higher than RTS in KKS. 
? *****************************************
create;SEC_y1a=b_b1+b_b11*b1+b_b12*b2+b_d11*a1+b_d12*a2+b_g11*g1$
create;SEC_y2a=b_b2+b_b12*b1+b_b22*b2+b_d21*a1+b_d22*a2+b_g12*g1$
create;SEC_ya=SEC_y1a+SEC_y2a$
create; RTSa=1/SEC_ya$
dstat; rhs=RTS, RTSa $

? *************
? Marginal cost
? *************
create;MC=SEC_y*OPEX/(TA)$
create;MCb=SEC_y*PTOC/(TA)$  /* PTOC is optimal Operatig Costs */
dstat;rhs=MC*,SEC_y,CE,*TOC,TA$
? ****************************
? Calculating technical change
? ****************************
calc;b_g23=(-1)*(b_g21+b_g22)$ 	/* recovering parameters on w3 */			
create;tec_pu=b_g1+b_g2*2*t$					
create;tec_sa=b_g11*b1+b_g12*b2$		
create;tec_nn=b_g21*a1+b_g22*a2+b_g23*a3$			
create;tec_to=tec_pu+tec_sa+tec_nn$
dstat;rhs=tec_to,tec_pu,tec_sa,tec_nn$
? ******************************************************
? Profit frontier p estimates with homogeneity as in KKS
? ******************************************************

sample;all$
Skip$
FRONTIER;lhs=lnPBT;rhs=one,SFA,lnNPI;Eff=pu;tlg=.0001;tlb=0.0001;tlf=0.0001;wts=wts;keep=PlnPBT;res=res$
create;PE=exp(-pu)$
create;PPBT=exp(PlnPBT)*w3$
create;APBT=PPBT/TA$
dstat;rhs=PE; Str=YEAR$

? *********************************************************
? Profit frontier p estimates without homogeneity in prices
? *********************************************************
sample;all$
Skip$
create;lnPBTa=log(pbtneg)$
create;lnNPIa=log(npdpbt)$
create;lny1=log(y1)$
create;lny2=log(y2)$
create;lnw1=log(w1)$
create;lnw2=log(w2)$
create;lnw3=log(w3)$
create;lnz1=log(z)$
create;ln5y1y1=0.5*lny1*lny1$
create;lny1y2=lny1*lny2$
create;ln5y2y2=0.5*lny2*lny2$
create;ln5w1w1=0.5*lnw1*lnw1$
create;lnw1w2=lnw1*lnw2$
create;lnw1w3=lnw1*lnw3$
create;ln5w2w2=0.5*lnw2*lnw2$
create;lnw2w3=lnw2*lnw3$
create;ln5w3w3=0.5*lnw3*lnw3$
create;ln5z1=0.5*lnz1*lnz1$
create;lny1w1=lny1*lnw1$
create;lny1w2=lny1*lnw2$
create;lny1w3=lny1*lnw3$
create;lny2w1=lny2*lnw1$
create;lny2w2=lny2*lnw2$
create;lny2w3=lny2*lnw3$
create;lny1z1=lny1*lnz1$
create;lny2z1=lny2*lnz1$
create;lnw1z1=lnw1*lnz1$
create;lnw2z1=lnw2*lnz1$
create;lnw3z1=lnw3*lnz1$
create;t=year-1975$
create;t2=t*t$
create;lny1t=lny1*t$
create;lny2t=lny2*t$
create;lnw1t=lnw1*t$
create;lnw2t=lnw2*t$
create;lnw3t=lnw3*t$
create;lnz1t=lnz1*t$
namelist; SFAa=lny1, lny2, lnw1,lnw2, lnw3, lnz1, ln5y1y1, lny1y2, ln5y2y2, ln5w1w1, lnw1w2, lnw1w3, ln5w2w2, lnw2w3, 
               ln5w3w3, lny1w1, lny1w2, lny1w3, lny2w1, lny2w2, lny2w3, lny1t,lny2t, lnw1t,lnw2t,lnw3t, t, t2$

FRONTIER;lhs=lnPBTa;rhs=one,SFAa,lnNPIa;Eff=pua;tlg=.0001;tlb=0.0001;tlf=0.0001;wts=wts;keep=PlnPBTa;res=res$
create;PEa=exp(-pua)$
create;PPBTa=exp(PlnPBTa)$
create;APBTa=PPBTa/TA$
dstat;rhs=PEa; Str=YEAR$

? ************************
? Calculate Lerner indices 
? ************************
create;p=rev/ta$
create;Lerner_1=(p-MC_OLS)/p$ /*Original KKS*/
create;Lerner_2=(p-MC_OLSb)/p$  /* MCb corrects the calculation of marginal costs */
create;Lerner_5=(APBT+ATOC-MC)/(APBT+ATOC)$ /* As computed by KKS*/
dstat;rhs=Lerner*,CE,PE; Str=YEAR$

?**********************************
?Compute Lerner Index
?without h.d.o in input prices for 
?the profit function.
?**********************************

create;MCa=SEC_ya*PTOC/(TA)$ /* Uses calculation for SEC_ya and Optimal TOC instead of actual costs */
dstat; rhs=MC*$

?MCa differs from MC since I use PTOC (Estimated optimal costs) and SEC_ya, instead of SEC_y.
?Only changing MC for MCa where I changed OPEX by ATOC (Optimal Cost) and SEC_y for SEC_ya . 
create; Lern5e=(APBTa+ATOC-MCa)/(APBTa+ATOC)$ /* MCa corrects SEC_y for SEC_ya and uses PTOC (Predicted Optimal Costs) instead of OPEX.*/
?Summary Statistics Table 5 in KKS
dstat;rhs=Lerner_2, Lern5e, CE,PEA$
dstat;rhs=Lerner_2, Lern5e, CE,PEA; Str=YEAR$
?Summary statistics Table 1 in replication note.
dstat; rhs=Lerner_1, Lerner_5, CE, PE; Str=YEAR$

?Export to Stata:
WRITE; year, entity, PE,PEA,CE, Lern5e, Lerner_2, ta, y1, y2;
 file="C:\results.txt"$




