Announcement

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

  • Convert the result from -table- to a matrix

    Dear All:

    This might be obvious to some of you, but I could not find a direct command that allows me to do so.

    I would like to convert the table produced from -table- to a simple matrix for future use. The Stata code goes as below.
    Code:
    table percentile1 percentile2
    ...Stata output...
    If I could use tabulate, it would be easy for me to do so by using
    Code:
    tab percentile1 percentile2, matcell(matrix)
    ...Stata output...
    However, -tabulate- has value limitation, which prevents me from doing so.

    I am wondering if the -table- command also has some simple option like the -tabulate- command? Or is there any alternative (quick) way I could do the conversion?

    Thank you and I look forward to hearing from you!

    Best,
    Long

  • #2
    I am wondering if the -table- command also has some simple option like the -tabulate- command?
    No and additionally, table does not leave behind results in e() or r()

    Or is there any alternative (quick) way I could do the conversion?
    Yes, use collapse. See the example below

    Code:
    sysuse auto, clear
    *OUTPUT FROM TABLE
    table rep78 foreign
    *USING COLLAPSE
    qui tab foreign, gen(origin)
    drop if missing(rep78)
    collapse (sum) origin*, by(rep78)

    Res.:

    Code:
    . table rep78 foreign
    
    ------------------------------
    Repair    |
    Record    |      Car type    
    1978      | Domestic   Foreign
    ----------+-------------------
            1 |        2          
            2 |        8          
            3 |       27         3
            4 |        9         9
            5 |        2         9
    ------------------------------
    
    
    . collapse (sum) origin*, by(rep78)
    
    . l
    
         +---------------------------+
         | rep78   origin1   origin2 |
         |---------------------------|
      1. |     1         2         0 |
      2. |     2         8         0 |
      3. |     3        27         3 |
      4. |     4         9         9 |
      5. |     5         2         9 |
         +---------------------------+

    Comment


    • #3
      Thanks, Andrew Musau! I guess following your suggestion, I would then make the variable to a matrix as my final step.

      I have just come up with an idea as well. I would do -tabulate , matcell(matrix)- twice for the first 50 percentiles and the second 50 percentiles, separately. Finally, I could combine the two matrices by the simple matrix operations in Stata.

      Comment


      • #4
        Thanks, Andrew Musau! I guess following your suggestion, I would then make the variable to a matrix as my final step
        Yes

        Code:
        mkmat rep78 origin*, mat(X)
        mat list X

        Comment


        • #5
          You can also use asdoc for this. asdoc can be downloaded from SSC. asdoc will not only send the output to a Word file but also leave behind a matrix named as st_mat_main

          Code:
          ssc install asdoc, replace
          
          sysuse auto, clear
          
          asdoc table rep78 foreign, replace
          
          
          matlist st_mat_main
          
                       |        c1         c2
          -------------+----------------------
                    r1 |         2          .
                    r2 |         8          .
                    r3 |        27          3
                    r4 |         9          9
                    r5 |         2          9
          For those who are not yet familiar with asdoc, it can be downloaded from SSC and can be used with almost all Stata commands. Here is a short blog post that shows how asdoc can be used with any Stata command http://fintechprofessor.com/2018/02/...basic-example/. You can also watch several YouTube videos that show the use of asdoc https://www.youtube.com/watch?v=zdI6...LwodAk2oqLYhr-

          Code:
          * For installation of the stable version 
          ssc install asdoc
          
          * For installation of the new beta version
          net install asdoc, from(http://fintechprofessor.com) replace
          help asdoc

          Regards
          --------------------------------------------------
          Attaullah Shah, PhD.
          Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
          FinTechProfessor.com
          https://asdocx.com
          Check out my asdoc program, which sends outputs to MS Word.
          For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

          Comment


          • #6
            Thanks Attaullah Shah! This looks cool!

            Comment


            • #7
              Originally posted by Attaullah Shah View Post
              You can also use asdoc for this. asdoc can be downloaded from SSC. asdoc will not only send the output to a Word file but also leave behind a matrix named as st_mat_main

              Code:
              ssc install asdoc, replace
              
              sysuse auto, clear
              
              asdoc table rep78 foreign, replace
              
              
              matlist st_mat_main
              
              | c1 c2
              -------------+----------------------
              r1 | 2 .
              r2 | 8 .
              r3 | 27 3
              r4 | 9 9
              r5 | 2 9
              For those who are not yet familiar with asdoc, it can be downloaded from SSC and can be used with almost all Stata commands. Here is a short blog post that shows how asdoc can be used with any Stata command http://fintechprofessor.com/2018/02/...basic-example/. You can also watch several YouTube videos that show the use of asdoc https://www.youtube.com/watch?v=zdI6...LwodAk2oqLYhr-

              Code:
              * For installation of the stable version
              ssc install asdoc
              
              * For installation of the new beta version
              net install asdoc, from(http://fintechprofessor.com) replace
              help asdoc
              Can you please check, in my result my columns are shiftet. That is, the columns are not in the expected position.

              My command is

              asdoc table row_var if condition==1, contents(mean v1 sd v1 mean v2 sd v2 ) by(area_var) row

              Comment


              • #8
                Shoummo Sen Gupta Please post some data and the output from the asdoc command and point out what is unexpected in the output.
                Regards
                --------------------------------------------------
                Attaullah Shah, PhD.
                Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
                FinTechProfessor.com
                https://asdocx.com
                Check out my asdoc program, which sends outputs to MS Word.
                For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

                Comment


                • #9
                  Another option is to use the estpost command from the estout package. estpost tab return the information in three different matrices which you can see using ereturn list.
                  Code:
                  sysuse auto
                  estpost tab foreign
                  ereturn list
                  Using this little program after running estpost tab varname, you can store the three single matrices in a matrix called "tblvarname" to report the numbers in a putdocx document.
                  Code:
                  program define combine_matrices
                      * Capture the name of the variable
                      local varname : word 1 of `e(depvar)'
                  
                      * Extract the matrices and rename them using the variable name
                      matrix `varname'_b = e(b)'
                      matrix `varname'_pct = e(pct)'
                      matrix `varname'_cumpct = e(cumpct)'
                  
                      * Combine the matrices into one
                      matrix tbl`varname' = `varname'_b, `varname'_pct, `varname'_cumpct
                  
                      * Set the column names
                      matrix colnames tbl`varname' = Freq Perc Cumul
                  
                      // Display the combined matrix
                      matlist tbl`varname'
                  end
                  __________________________________________________ __________

                  Cheers, Felix
                  Stata Version: MP 18.0
                  OS: Windows 11

                  Comment


                  • #10
                    This thread predates Stata 17 and the new table and collect suite of commands. Here is a way to do this using collect and import excel.

                    Code:
                    sysuse auto, clear
                    table rep78 foreign
                    collect export mytable.xls, replace
                    import excel "mytable.xls", sheet("Sheet1") cellrange(A2) firstrow clear
                    drop in 1
                    qui ds A, not
                    mkmat `r(varlist)', rownames(A) mat(wanted)
                    mat list wanted
                    Res.:

                    Code:
                    . table rep78 foreign
                    
                    ------------------------------------------------
                                       |          Car origin        
                                       |  Domestic   Foreign   Total
                    -------------------+----------------------------
                    Repair record 1978 |                            
                      1                |         2                 2
                      2                |         8                 8
                      3                |        27         3      30
                      4                |         9         9      18
                      5                |         2         9      11
                      Total            |        48        21      69
                    ------------------------------------------------
                    
                    
                    
                    . mat list wanted
                    
                    wanted[6,3]
                             Domestic   Foreign     Total
                        __1         2         .         2
                        __2         8         .         8
                        __3        27         3        30
                        __4         9         9        18
                        __5         2         9        11
                    __Total        48        21        69
                    Last edited by Andrew Musau; 01 Aug 2024, 05:20.

                    Comment

                    Working...
                    X