Announcement

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

  • twoway table format

    Dear users is there a way to format decimal places format for twoway table: rounding frequency and one decimal place for percent. Is it possible to shows numbers without comma separator ?

    Code:
    *number 
    . ta b4 odfunimp[iw=wt]
    
    
        sex of |      odforunimp
         child |        No        Yes |     Total
    -----------+----------------------+----------
          male | 70,538.99  69,611.51 | 140,150.5 
        female | 63,413.53  64,841.31 | 128,254.8 
    -----------+----------------------+----------
         Total | 133,952.5  134,452.8 | 268,405.3 
    
    
    *percent
    . ta b4 odfunimp[iw=wt], row nofr
    
        sex of |      odforunimp
         child |        No        Yes |     Total
    -----------+----------------------+----------
          male |     50.33      49.67 |    100.00 
        female |     49.44      50.56 |    100.00 
    -----------+----------------------+----------
         Total |     49.91      50.09 |    100.00
    Thank you !
    Stata 18/SE
    Best regards,
    Mukesh

  • #2
    In any version of Stata 17 and above, use the command table instead of tabulate or tab2, and then the option nformat()

    For instance:

    Code:
    . webuse nhanes2f, clear
    . table sex heartatk [iw = finalwgt]
    
    ------------------------------------------------------------
             |                 Prior heart attack              
             |  No heart attack   Had heart attack         Total
    ---------+--------------------------------------------------
    Sex      |                                                  
      Male   |       53,835,751          2,286,284    56,122,035
      Female |       59,696,785          1,178,437    60,875,222
      Total  |      113,532,536          3,464,721   116,997,257
    ------------------------------------------------------------
    
    . table sex heartatk [iw = finalwgt], nformat(%9.0f)
    
    ----------------------------------------------------------
             |                Prior heart attack              
             |  No heart attack   Had heart attack       Total
    ---------+------------------------------------------------
    Sex      |                                                
      Male   |         53835751            2286284    56122035
      Female |         59696785            1178437    60875222
      Total  |        113532536            3464721   116997257
    ----------------------------------------------------------
    
    . table sex heartatk [iw = finalwgt], stat(perc, across(heartatk))
    
    -------------------------------------------------------
             |               Prior heart attack            
             |  No heart attack   Had heart attack    Total
    ---------+---------------------------------------------
    Sex      |                                            
      Male   |            95.93               4.07   100.00
      Female |            98.06               1.94   100.00
      Total  |            97.04               2.96   100.00
    -------------------------------------------------------
    
    . table sex heartatk [iw = finalwgt], stat(perc, across(heartatk)) nformat(%2.1f)
    
    ------------------------------------------------------
             |              Prior heart attack            
             |  No heart attack   Had heart attack   Total
    ---------+--------------------------------------------
    Sex      |                                            
      Male   |             95.9                4.1   100.0
      Female |             98.1                1.9   100.0
      Total  |             97.0                3.0   100.0
    ------------------------------------------------------
    In Stata 18 or later, you may also want to explore the command dtable, which also has options for formatting the output.

    And finally, in Stata 19, you can use the new option collect to the command tabulate to post the table to a collection, which can then be formatted using the collect suite of commands.
    Last edited by Hemanshu Kumar; 26 Jun 2025, 12:39.

    Comment


    • #3
      Thank you! Hemanshu Kumar for your prompt response. Can you please help me to get n & percent in one table. For example, percentage next to number of frequency.
      Best regards,
      Mukesh

      Comment


      • #4
        Consider this:
        Code:
        webuse nhanes2f, clear
        
        table sex heartatk [iw = finalwgt], ///
            stat(sumw) ///
            stat(perc, across(heartatk)) ///
            nformat(%9.0f sumw) nformat(%2.1f perc) sformat("(%s%%)" perc)
            
        collect composite define freq_perc = sumw percent, trim replace
        collect style header result[freq_perc], title(hide) level(hide)
        collect layout (sex) (heartatk#result[freq_perc])
        which produces:
        Code:
        . collect preview
        
        ---------------------------------------------------------------------
                 |                      Prior heart attack                  
                 |    No heart attack   Had heart attack                Total
        ---------+-----------------------------------------------------------
        Sex      |                                                          
          Male   |   53835751 (95.9%)     2286284 (4.1%)    56122035 (100.0%)
          Female |   59696785 (98.1%)     1178437 (1.9%)    60875222 (100.0%)
          Total  |  113532536 (97.0%)     3464721 (3.0%)   116997257 (100.0%)
        ---------------------------------------------------------------------
        Note: I replaced a previous post that suggested using dtable since you need row percentages, which dtable does not allow.
        Last edited by Hemanshu Kumar; 26 Jun 2025, 13:59.

        Comment

        Working...
        X