Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • putexcel in a loop

    Dear All,

    I have the following do file:

    Code:
    putexcel set capm.xlsx, sheet(capm) replace
       putexcel (A1:C1)=("Simple CAPM"), border(bottom, double) merge hcenter vcenter bold 
       putexcel A2=("Stock 1"), hcenter border(bottom) italic  
       putexcel B2=("alpha"), hcenter border(bottom) italic
       putexcel C2=("beta"), hcenter border(bottom) italic
       
    local asset "ermicrosoft eroracle erge erford"
    
    foreach i of local asset{
        reg `i' ersandp, r
        local alpha_`i'=_b[_cons]
        local beta_`i'=_b[ersandp]
        local m 2
        local ++m
        putexcel A`m'=("`i'"), hcenter 
        putexcel B`m'=("`alpha_`i''"), hcenter
        putexcel C`m'=("`beta_`i''"), hcenter
    }
    I would like to estimate the equation:

    Code:
    reg `i' ersandp, r
    for each asset in local asset. Then I want to save the estimations of alpha and beta and export them in an excel file. I use

    Code:
    local m 2
    local ++m
    to ask Stata to put the estimated parameters for each asset in a different row in Excel. However, in the excel file I see only the results for the last asset (ford). The logic is clear: if I set m=2 and then I specify local ++m at each estimation ++m is equal to 3. But how can I set the command such that in row 3 I have the results for microsoft, in row 4 I have the results oracle and so on?

    Thanks for your help.

    Dario

  • #2
    Guys,

    I was able to solve the issue above. I used:

    Code:
    putexcel set capm.xlsx, sheet(capm) replace
       putexcel (A1:C1)=("Simple CAPM"), border(bottom, double) merge hcenter vcenter bold 
       putexcel A2=("Stock 1"), hcenter border(bottom) italic  
       putexcel B2=("alpha"), hcenter border(bottom) italic
       putexcel C2=("beta"), hcenter border(bottom) italic
       
    local asset "microsoft oracle ge ford"
    local k: word count `asset'
    local m 2
    foreach i of local asset{
        reg `i' ersandp, r
        local alpha_`i'=_b[_cons]
        local beta_`i'=_b[ersandp]
        local ++m
        putexcel A`m'=("`i'"), hcenter 
        putexcel B`m'=("`alpha_`i''"), hcenter
        putexcel C`m'=("`beta_`i''"), hcenter
    }

    Comment

    Working...
    X