Announcement

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

  • Store results from dtable to a matrix

    I want to be able to report some numbers from a summary table in text using putdocx. My preferred command is the dtable command since it contains both summary statistics by groups and the associated tests for both continuous and categorical variables. I read the manual, and used return list and ereturn list to see if dtable returns a matrix I could use, but to no avail. The dtable manual describes how to use the collect command can see that collect preview I want to save the results from dtable to a matrix. I also tried to find if I can save a collection to a matrix, but I also had no luck finding a solution.

    Code:
    sysuse auto
    dtable mpg, by(foreign)
    collect label list result
    
    matrix summary = ?
    
    putdocx begin
    putdocx paragraph
    putdocx text ("In total our dataset features `:di %5.0f `=matrix["N", "Domestic"]' domestic and `:di %5.0f `=matrix["N", "Foreign"]' foreign cars.")
    putdocx save Sample.docx
    The Word-Document should then contain: "In total our dataset features 52 domestic and 22 foreign cars."
    Last edited by Felix Kaysers; 01 Aug 2024, 06:36. Reason: added tags
    Cheers,
    Felix
    Stata Version: MP 18.0
    OS: Windows 11

  • #2
    You can input the table directly using putdocx collect, but I suspect that if you want to access the individual statistics to write them inside a text you will have to get them separately.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thank you Maarten Buis. With dtable, I could also just use
      Code:
      dtable mpg, by(foreign) export(DescriptiveTable.docx)
      Unfortunately, you are correct that I want to access the individual statistics to input them into text.
      Cheers,
      Felix
      Stata Version: MP 18.0
      OS: Windows 11

      Comment


      • #4
        one way to read elements from the collection:

        Code:
        clear all
        
        sysuse auto
        dtable mpg, by(foreign)
        
        tempfile collection
        collect export `collection', as(xlsx) noopen
        
        tempname temp_frame_collection
        frame create `temp_frame_collection'
        
        frame `temp_frame_collection' {
        
            import excel using `collection'
            mata : collection = st_sdata(.,.)'
            getmata (variable value N mpg)=collection, replace
            list
        
            drop if N=="N"
        
            foreach v of varlist N mpg {
                
                 split `v'
            }
        
            assert !mi(N1) & real(N1) == int(real(N1))
        
            forvalues i=1/`c(N)' {
                
                scalar `= "n_" + value[`i'] ' = N1[`i']
                scalar `= "p_" + value[`i'] ' = N2[`i']
            }
        
            local stat N? mpg?
        
            foreach v of varlist `stat' {
        
                replace `v' = regexreplaceall(`v', "[^.\d]","")
            }
        
            destring `stat', replace
            mkmat `stat', matrix(N_mpg) rownames(value)
        }
        
        scalar dir
        matlist N_mpg
        
        Code:
        list
        
             +-------------------------------------------------------------------------------------------------------------------------+
             |             A                B                C                D     variable      value             N              mpg |
             |-------------------------------------------------------------------------------------------------------------------------|
          1. |                     Car origin       Car origin       Car origin                                     N    Mileage (mpg) |
          2. |                       Domestic          Foreign            Total   Car origin   Domestic    52 (70.3%)   19.827 (4.743) |
          3. |             N       52 (70.3%)       22 (29.7%)      74 (100.0%)   Car origin    Foreign    22 (29.7%)   24.773 (6.611) |
          4. | Mileage (mpg)   19.827 (4.743)   24.773 (6.611)   21.297 (5.786)   Car origin      Total   74 (100.0%)   21.297 (5.786) |
             +-------------------------------------------------------------------------------------------------------------------------+
        Code:
        scalar dir
           p_Total = (100.0%)
           n_Total = 74
         p_Foreign = (29.7%)
         n_Foreign = 22
        p_Domestic = (70.3%)
        n_Domestic = 52
        Code:
        . matlist N_mpg
        
                     |        N1         N2       mpg1       mpg2 
        -------------+-------------------------------------------
            Domestic |        52       70.3     19.827      4.743 
             Foreign |        22       29.7     24.773      6.611 
               Total |        74        100     21.297      5.786
        Last edited by sladmin; 08 Aug 2024, 14:04.

        Comment

        Working...
        X