Announcement

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

  • Exporting results to Excel using code (not manually copy paste) - How?

    Hi!
    I am quite new to Stata so there may be some basic elements that I have not learnt yet. I have some knowledge of Python programming from before, but quite limited. With that said, this is my issue at hand: I do not want to manually copy paste results from Stata (such a waste of time!) and the export function in Stata seems to not cover all areas. So far, I have mainly used the command putexcel to export rscalars, but not many values are saved as such.

    In practise, this is what I want to do. At this point I am not doing anything fancy, just looking at baseline data. I need to get a lot of descriptives (mean etc), run basic tests and I want to do a nice summary to 1. get a good over-view and 2. present to others. And I am using excel/word.

    1. I use e.g. the command tabmore to get mean and confidence intervals. After each tabmore I want to export mean and confidence interval to excel and I do not want to manually copy paste. How can I do this??

    2. I do e.g. ANOVA tests and quite a lot of them. The test value is stored as an rscalar, but not the p-value. How can I export the p-value??

    3. I use tabulate to run Ch2 test. The test value and p-value are saved as rscalars and can be exported. But I also want the number/proportion of observations in each subgroup. How can I export those?

    4. An extra question: At this point I have not found how to do a loop to go through different variables. So in order to not having to write all commands I write the code in excel where I can change from one variable to another using links and then copy it all into a do file. Is there a loop that can do this in Stata? I have only found loops that go through a series of numbers, like from 1 to 10, not through a list of variables.

    Any ideas? Thank you!

    PS.
    I have searched in two, more or less basic books, on Stata, I have searched forums and youtube, asked my colleagues (who use the manual copy paste method), so perhaps there is no tool for this. It feels like a fundamental question and to post it here is my final attempt before I have to copy paste (buhaaa). However, I am using Stata 13. Perhaps version 14 have these options if not 13?

  • #2
    There's a command called putexcel: http://www.stata.com/manuals13/pputexcel.pdf
    Probably better, eststo and estout are very good packages for sticking stuff in excel or in formatted tables in word: http://repec.org/bocode/e/estout/esttab.html
    Get them by doing: ssc install estout

    edit: I see youre asking for quite a bit more details actually. I typed these options after just reading the title. I'm not so sure about the specifics of the values generated by the commands you ask about.
    Last edited by Jorrit Gosens; 21 Jun 2016, 05:13.

    Comment


    • #3
      Welcome to Statalist, and to using Stata!

      With regard to your question #4, it appears to me that you have overlooked some of the fine documentation supplied in Stata. When I began using Stata in a serious way, I started by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. All of these manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through Stata's Help menu. The objective in doing this was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax.

      Comment


      • #4
        Code:
        
        sysuse auto, clear
        
        *4. An extra question: At this point I have not found how to do a loop to go through different variables. So in order to not having to write all commands I write the code in excel where I can change from one variable to another using links and then copy it all into a do file. Is there a loop that can do this in Stata? I have only found loops that go through a series of numbers, like from 1 to 10, not through a list of variables.
        
        **take a look at:
        man foreach
        
        
        
        
        *1. I use e.g. the command tabmore to get mean and confidence intervals. After each tabmore I want to export mean and confidence interval to excel and I do not want to manually copy paste. How can I do this?? 
        
        
        findit tabmore //turns up zero hits ?
        
        
         putexcel set "test.xls", replace
            putexcel A1 = ("Variable")
            putexcel B1 = ("Mean")
            putexcel C1 = ("LL")
            putexcel D1 = ("UL")
        local i = 2 //starts table below at row 2
        foreach j of varlist price mpg t* {
            mean `j'
            mat A = r(table)
            mat li A
            **export mean and CI to excel via putexcel:
            putexcel B`i'=("`j'")
            putexcel C`i'= (A[1,1]) //mean
            putexcel D`i'= (A[5,1]) //lower CI
            putexcel E`i'= (A[6,1]) //upper CI
            local i `++i' //see help for macro functions, advances i +1
            }
            
        
        *2. I do e.g. ANOVA tests and quite a lot of them. The test value is stored as an rscalar, but not the p-value. How can I export the p-value?? 
        anova price for
        return li
        mat A = r(table)
        mat li A
        putexcel A8 = ("P value from Model")
        putexcel B8 = (A[4,2])
        
        *3. I use tabulate to run Ch2 test. The test value and p-value are saved as rscalars and can be exported. But I also want the number/proportion of observations in each subgroup. How can I export those?
        
        ta mpg for, chi2 
        return li
        putexcel A10 = ("Ttest values")
        putexcel B10 = (r(chi2))
        putexcel C10 = (r(p))
        Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

        Comment

        Working...
        X