Hello, I am new to Stata and trying to loop code to calculate Idiosyncratic risk for companies in separate Excel sheets. This is the code I want to loop for each sheet in my excel workbook. All sheets are named after the company which information it contains. I realise it should not be particularly difficult but being new to Stata, I simply cannot figure out how to make it work.
import excel myfilepath/myfilename.xlsx, sheet("my_sheetname") firstrow
// Estimate the beta using the CAPM
regress Stockreturn Marketreturn
estimates store beta_coeff // store the beta coefficient
// Specify the beta coefficient
scalar beta = _b[Marketreturn]
// Calculate the expected return using the CAPM
summ Marketreturn
scalar Marketreturn_mean = r(mean)
scalar expected_return = 0.03 + beta * (Marketreturn_mean - 0.03)
// Calculate the idiosyncratic risk using the CAPM
summarize Stockreturn
scalar Stockreturn_var = r(Var)
scalar idiosyncratic_risk = Stockreturn_var - beta^2 * (Marketreturn_mean - 0.03)^2
// Display the results
display "Beta: " beta
display "Expected return: " expected_return
display "Idiosyncratic risk: " idiosyncratic_risk
Thank you in advance
Anton
import excel myfilepath/myfilename.xlsx, sheet("my_sheetname") firstrow
// Estimate the beta using the CAPM
regress Stockreturn Marketreturn
estimates store beta_coeff // store the beta coefficient
// Specify the beta coefficient
scalar beta = _b[Marketreturn]
// Calculate the expected return using the CAPM
summ Marketreturn
scalar Marketreturn_mean = r(mean)
scalar expected_return = 0.03 + beta * (Marketreturn_mean - 0.03)
// Calculate the idiosyncratic risk using the CAPM
summarize Stockreturn
scalar Stockreturn_var = r(Var)
scalar idiosyncratic_risk = Stockreturn_var - beta^2 * (Marketreturn_mean - 0.03)^2
// Display the results
display "Beta: " beta
display "Expected return: " expected_return
display "Idiosyncratic risk: " idiosyncratic_risk
Thank you in advance
Anton
Comment