Announcement

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

  • Exporting results to excel

    Dear Stata Users,

    As a new user, I would like some help with exporting results directly into excel after running the loop below.

    local y "agegroup gender duration"
    foreach var of local y {
    forvalues i = 1/3 {
    proportion fma if finaldd == `i', over(`var') percent
    matrix `var'_`i' = r(table)
    }
    }

    The results of the above loop are stored in 9 different matrix tables. An example of one matrix table stored is given below - 1 represents male and 2- female.
    gender_1[9,4]
    0.fma@ 0.fma@ 1.fma@ 1.fma@
    1.gender 2.gender 1.gender 2.gender
    b 99.172502 94.707091 .82749841 5.2929085
    se .1142776 .5075362 .1142776 .5075362
    t 867.82097 186.60165 7.2411249 10.428632
    pvalue 0 0 4.851e-13 2.638e-25
    ll 98.915599 93.619234 .63106954 4.381839
    ul 99.36893 95.618161 1.0844012 6.3807662
    df 8229 8229 8229 8229
    crit 1.9602523 1.9602523 1.9602523 1.9602523
    eform 0 0 0 0

    I was going to ask if there was a way to extract only the information in bold and then export into excel. Using putexcel means writing too many lines of commands and I want to believe there is a more straight forward way of doing this.

    Looking forward to your replies. Thanks.

  • #2
    Next time please put some data example using dataex. It saves others' time and you get more relevant reply. See FAQ section on how to do that. Here is a fictitious example:

    Code:
    ***Fictitious data************
    
    clear
    
    set obs  30
    
    g finaldd = int((3-1+1)*runiform()+1)
    so finaldd
    
    loc x fma agegroup gender duration
    foreach v of loc x {
        g `v' = runiform()>.5
    }
    
    lab define agegrp  0 "20-30yrs" 1 "30-40yrs"
    lab val agegroup agegrp
    lab define gender 0 "Female" 1 "Male"
    lab val gender gender
    lab define dur 0 "3 weeks" 1 ">3 weeks"
    lab val duration dur
    lab var agegroup "Age-group"
    lab var gender "Gender"
    lab var duration "Duration"
    
    ***********Transfer to excel*******************
    
    putexcel set test.xlsx, sheet(test) modify
    loc rownum = 4
    
    local y "agegroup gender duration"
    
    foreach var of local y {
        forvalues i = 1/3 {
            proportion fma if finaldd == `i', over(`var') percent
            
            mat k = r(table)
                
                foreach j of numlist 3 4 {
                    loc b     : di %3.2f k[1, `j']
                    loc ll     : di %3.2f k[5, `j']
                    loc ul     : di %3.2f k[6, `j']
                    
                    putexcel A`rownum' = "`:var lab `var''"
                    putexcel B`rownum' = "Finaldd = `i'"
                    putexcel C`rownum' = "`b' (`ll'–`ul')"
                    
                    loc ++rownum
                }
        }    
    }
    Roman

    Comment

    Working...
    X