Announcement

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

  • Saving data from bysort tab

    Dear all,

    How do I save the data from a tabulate command?

    I am using the following code where I run a logistic regression and then want to tabulate a variable by another variable based on the population from the logistic regression.

    logistic DV IV if include==1 & Dos1== 1,or
    bysort Region: tab Sex if e(sample)

    How do I save the results for the bysort and tab command? I want to them use these data for a table.

    Best,
    Adam


  • #2
    Code:
    tab Region Sex if e(sample) 
    
    table Region Sex if e(sample)
    should both be more convenient.

    Comment


    • #3
      Comment deleted.
      Last edited by Salvatore Viola; 30 Aug 2022, 11:38.

      Comment


      • #4
        Thanks Nick,

        I get the raw numbers but is there also a way to get the percentages in such a table?

        Comment


        • #5
          Try
          Code:
          tab Region Sex if e(sample), nofreq cell
          If you want the column (row) percentages to add up to 100, use -column- (-row-) instead of -cell-. See also
          Code:
          help tabulate twoway

          Comment


          • #6
            Thanks,
            This works! but how do I save/export these data into a stata table?

            Comment


            • #7
              OP, you are not asking very well your question, so it is not clear how exactly you want to save these things. (There is no such thing as a "save as Stata table"). You can save as data, or you can save as a matrix.

              Check out the -tabulate- option matcell(matname), something like

              Code:
              tabulate region sex if e(sample), matcell(MyMat)
              might do the trick to save to a matrix.

              Or if this is not what you want to do, try again to explain what you want exactly.

              Comment


              • #8
                Here is an example of what I have on mind:

                Code:
                . sysuse auto
                (1978 automobile data)
                
                . tabulate rep foreign, matcell(MyMat)
                
                    Repair |
                    record |      Car origin
                      1978 |  Domestic    Foreign |     Total
                -----------+----------------------+----------
                         1 |         2          0 |         2 
                         2 |         8          0 |         8 
                         3 |        27          3 |        30 
                         4 |         9          9 |        18 
                         5 |         2          9 |        11 
                -----------+----------------------+----------
                     Total |        48         21 |        69 
                
                . matlist MyMat
                
                             |        c1         c2 
                -------------+----------------------
                          r1 |         2          0 
                          r2 |         8          0 
                          r3 |        27          3 
                          r4 |         9          9 
                          r5 |         2          9

                Comment

                Working...
                X