Announcement

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

  • Excel Table split by socio-demographics using Putexcel

    Hello fellow Stata community,

    I´m looking for a way to automate my Analysis Output to Excel using Putexcel. I already found this Code by eric_a_booth that helps me create summary tables for each of the four example variables, each on an own sheet:
    foreach j of varlist var1 var2 d_var1 d_var2{
    di `"`j'"'
    su `j'
    putexcel A3=rscalars using `"test.xlsx"', sheet("S_`j'") modify keepcellf
    putexcel A1=(`" Example for `j' "') using `"test.xlsx"', sheet("S_`j'") modify keepcellf
    }


    di `"{browse `"test.xlsx"': Click to open `"test.xlsx"' }"'

    What I´m now looking for, would be an extension of this. My goal would be creating automated tables like the one attached, displaying the values for each variable over the values of a socio-demographic variable. In a shortened try, I programmed the following:
    sum var1
    *return list
    putexcel A1=("Table 1") B3=("Obs") C3=("Total") D3=("Female") E3=("Male") F3=("Divers") using results, replace
    putexcel C4=matrix(r(mean)*100) using results, modify
    sum var1 if gender==1
    putexcel D4=matrix(r(mean)*100) using results, modify
    sum var1 if gender==2
    putexcel E4=matrix(r(mean)*100) using results, modify
    sum var1 if gender==3
    putexcel F4=matrix(r(mean)*100) using results, modify
    ...

    Since I want to run this over dozens of var´s it seems inevitable to create some kind of a loop.

    I hope the example table helps understanding where i´m trying to head with this, otherwise I´ll try to further specify my challenge.
    Thanks in advance for any help given, I´d be more than thankful for any advise.

    Regards
    Marvin


    Click image for larger version

Name:	Example_Outputdesign.PNG
Views:	2
Size:	41.0 KB
ID:	1561234

    Attached Files

  • #2
    See if this code helps https://www.statalist.org/forums/for...excel-column-z

    Comment

    Working...
    X