Announcement

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

  • Export table with only one category from one variable

    Dear Statalist,

    I am doing a twoway tab between an education variable = {1,2,3,4} and a bnary variable. I want the row percentages for binary_variable==1.

    This is my code:

    estimates clear
    estpost tab educ missjob if tem==2, nototal
    eststo tem0

    estpost tab educ missjob if tem==1, nototal
    eststo tem1


    esttab tem0 tem1 using "$OUT/Table_1'.tex", replace unstack booktabs ///
    cell(rowpct(fmt(2))) varlabels(`e(labels)') ///
    eqlabels(`e(eqlabels)') nonum collabels(none) ///
    mtitle("Without permit" "With permit")

    but I only want a table with 2 columns, like this:
    Click image for larger version

Name:	statalist.png
Views:	1
Size:	19.1 KB
ID:	1740206


  • #2
    estout is from SSC (FAQ Advice #12). Look at how the equation is named and then use the -drop()- option.

    Code:
    sysuse auto, clear
    qui sum price, d
    gen hiprice= price>r(p50)
    eststo m1: estpost tab rep78 foreign if !hiprice, nototal 
    eststo m2: estpost tab rep78 foreign if hiprice, nototal
    esttab m1 m2, replace unstack cell(rowpct(fmt(2))) nonum collabels(none)  
    esttab m1 m2, replace unstack cell(rowpct(fmt(2))) nonum collabels(none)  drop(Domestic:)
    Res.:

    Code:
    . esttab m1 m2, replace unstack cell(rowpct(fmt(2))) nonum collabels(none)  
    
    ----------------------------------------------------------------
                                                                    
                     Domestic      Foreign     Domestic      Foreign
    ----------------------------------------------------------------
    1                  100.00         0.00                          
    2                  100.00         0.00       100.00         0.00
    3                   88.24        11.76        92.31         7.69
    4                   50.00        50.00        50.00        50.00
    5                   40.00        60.00         0.00       100.00
    ----------------------------------------------------------------
    N                      34                        35             
    ----------------------------------------------------------------
    
    . 
    . esttab m1 m2, replace unstack cell(rowpct(fmt(2))) nonum collabels(none)  drop(Domestic:)
    
    --------------------------------------
                                          
                      Foreign      Foreign
    --------------------------------------
    1                    0.00             
    2                    0.00         0.00
    3                   11.76         7.69
    4                   50.00        50.00
    5                   60.00       100.00
    --------------------------------------
    N                      34           35
    --------------------------------------
    To see how the equation is named, just after the esttab command, run

    Code:
    mat l e(b)
    Res.:

    Code:
    . mat l e(b)
    
    e(b)[1,8]
         Domestic:  Domestic:  Domestic:  Domestic:   Foreign:   Foreign:   Foreign:   Foreign:
                2          3          4          5          2          3          4          5
    y1          4         12          6          0          0          1          6          6

    Comment


    • #3
      Alternatively, you can use the table command if you have Stata 17 or higher

      Code:
      . // open example data
      . sysuse nlsw88, clear
      (NLSW, 1988 extract)
      
      . // prepare one of the variables
      . gen byte urban = c_city + smsa
      
      . label define urban 2 "central city" ///
      >                    1 "suburban"     ///
      >                    0 "rural"
      
      . label value urban urban
      
      . label variable urban "urbanicity"
      
      .
      . // create a basic table
      . table (urban) (south union), nototal stat(percent, across(union)) nformat(%9.1f) sformat(%s%%) zerocounts
      
      -----------------------------------------------------
                     |           Lives in the south        
                     |      Not south            South     
                     |    Union worker       Union worker  
                     |  Nonunion   Union   Nonunion   Union
      ---------------+-------------------------------------
      urbanicity     |                                     
        rural        |     73.1%   26.9%      87.0%   13.0%
        suburban     |     72.0%   28.0%      86.8%   13.2%
        central city |     63.2%   36.8%      75.1%   24.9%
      -----------------------------------------------------
      
      . // modify the table to look the way we want
      . collect layout (urban) (south#union[1])
      
      Collection: Table
            Rows: urban
         Columns: south#union[1]
         Table 1: 4 x 2
      
      ---------------------------------------------
                     |       Lives in the south    
                     |     Not south          South
                     |  Union worker   Union worker
                     |         Union          Union
      ---------------+-----------------------------
      urbanicity     |                             
        rural        |         26.9%          13.0%
        suburban     |         28.0%          13.2%
        central city |         36.8%          24.9%
      ---------------------------------------------
      
      . collect style header south, title(hide)
      
      . collect style header urban, title(hide)
      
      . collect style header union, title(hide) level(hide)
      
      . collect preview
      
      ---------------------------------
                   |  Not south   South
      -------------+-------------------
      rural        |      26.9%   13.0%
      suburban     |      28.0%   13.2%
      central city |      36.8%   24.9%
      ---------------------------------
      . // we can now use -collect export- to export the table to LaTeX, Word, Excel, ...

      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Thank you both! It worked perfectly

        Comment

        Working...
        X