Announcement

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

  • how to display frequencies and percentages to whole numbers only in STATA SE/15.1

    Hi, 'm working with a data set of 58 variables and 795 observations. Most of the variables are categorical and I'm using the tab command to display frequencies.
    Stata provides these results to 2 decimal places, but I would like to display them as whole numbers only or to one decimal point only. I want to ensure the calculation is done in stata rather than me rounding up which may lead to inaccuracies.

    I am using the following commands:
    tab lat
    tab lat centre, column

    I have tried the following format commands:
    tab lat, column format(%9.1f)
    tab alt, column format(%3.0f)

    But I receive the error message "r(198)".

    What command shall I use?

    Example of data using dataex displayed below:

    input str5 id byte(disp opcl lat centre)
    "NG1" 2 2 1 1
    "NG2" 2 2 1 1
    "NG3" 2 2 2 1
    "NG4" 1 2 2 1
    "NG5" 2 2 1 1
    "NG6" 2 2 1 1
    "NG7" 2 2 1 1
    "NG8" 2 2 1 1
    "NG9" 2 2 1 1
    "NG10" 2 2 1 1
    "NGT11" 3 2 1 1
    "NG12" 2 2 2 1
    "NG13" 2 2 1 1
    "NG14" 3 2 1 1
    "NG15" 2 2 2 1
    "NG16" 2 2 2 1
    "NG17" 1 2 2 1
    "NG18" 3 1 1 1
    "NG19" 2 2 2 1
    "NG20" 2 2 1 1
    "NG21" 2 1 2 1
    "NG22" 1 2 1 1
    "NG23" 2 2 1 1
    "NG24" 2 2 1 1
    "NG25" 2 2 2 1
    "NG26" 1 2 2 1
    "NG27" 2 2 2 1
    "NG28" 2 2 1 1
    "NG29" 2 2 1 1
    "NG30" 1 2 2 1
    "NG31" 2 2 1 1
    "NG32" 1 2 2 1
    "NG33" 2 2 2 1
    "NG35" 2 2 2 1
    "NG36" 2 2 1 1
    "NG37" 1 2 1 1
    "NG38" 2 2 1 1
    "NG39" 2 2 1 1
    "NG40" 2 2 1 1
    "NG41" 1 2 2 1
    "NG42" 2 2 1 1
    "NG43" 2 2 1 1
    "NG44" 2 2 1 1
    "NG45" 1 2 1 1
    "NG46" 1 2 2 1
    "NG47" 2 2 1 1
    "NG48" 2 2 1 1
    "NG49" 2 2 2 1
    "NG50" 2 2 2 1
    "NG51" 2 2 2 1
    "NG52" 2 2 1 1
    "NG53" 2 2 1 1
    "NG54" 2 2 1 1
    "NG55" 2 2 2 1
    "NG56" 2 2 2 1
    "NG57" 2 2 1 1
    "NG58" 2 2 1 1
    "NG59" 3 2 1 1
    "NG60" 1 2 1 1
    "NG61" 2 2 1 1
    "NG62" 2 2 2 1
    "NG63" 2 2 2 1
    "NG64" 2 2 1 1
    "NG65" 2 2 1 1
    "NG66" 1 2 1 1
    "NG67" 1 2 1 1
    "NG68" 1 2 1 1
    "NG69" 2 2 2 1
    "NG70" 2 2 1 1
    "NG71" 2 2 1 1
    "NG72" 2 2 1 1
    "NG73" 1 2 1 1
    "NG74" 2 2 1 1
    "NG75" 2 2 1 1
    "NG76" 2 2 1 1
    "NG77" 1 2 1 1
    "NG78" 1 2 2 1
    "NG79" 2 2 1 1
    "NG80" 2 2 1 1
    "NG81" 2 2 1 1
    "NG82" 2 2 2 1
    "NG83" 1 2 2 1
    "NG84" 2 2 1 1
    "NG85" 2 2 1 1
    "NG86" 3 2 1 1
    "NG87" 2 2 1 1
    "NG88" 2 2 1 1
    "NG89" 1 2 1 1
    "NG90" 1 2 2 1
    "NG91" 1 2 2 1
    "NG92" 2 2 1 1
    "NG93" 2 2 1 1
    "NG94" 2 2 2 1
    "NG95" 2 2 1 1
    "NG96" 2 2 2 1
    "NG97" 1 2 1 1
    "NG98" 1 2 2 1
    "NG99" 2 2 1 1
    "NG100" 1 2 2 1
    "NG101" 2 2 1 1
    end
    label values disp disp
    label def disp 1 "undisplaced", modify
    label def disp 2 "some", modify
    label def disp 3 "marked", modify
    label values opcl opcl
    label def opcl 1 "open", modify
    label def opcl 2 "closed", modify
    label values lat lat
    label def lat 1 "right", modify
    label def lat 2 "left", modify
    label values centre centre
    label def centre 1 "Nottingham", modify
    [/CODE]


    Tables displayed using tab command attached as a picture.

    Many thanks,
    Attached Files

  • #2
    The error here should not be surprising. format() isn't an option of tabulate. No such option is documented and while undocumented options exist the error message shows that format() is not one such.

    The following shows one of many work-arounds. With just two categories, the percent of one category is 100 times the mean of an indicator for that category and the percent of the other category is just the complement in 100%. With such a variable we can feed results to table which does allow a format() option.

    I show this first with the auto data. Your data example is helpful but not a full test as it doesn't extend beyond Nottingham.

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . tab rep78 for, row
    
    +----------------+
    | Key            |
    |----------------|
    |   frequency    |
    | row percentage |
    +----------------+
    
        Repair |
        Record |       Car type
          1978 |  Domestic    Foreign |     Total
    -----------+----------------------+----------
             1 |         2          0 |         2 
               |    100.00       0.00 |    100.00 
    -----------+----------------------+----------
             2 |         8          0 |         8 
               |    100.00       0.00 |    100.00 
    -----------+----------------------+----------
             3 |        27          3 |        30 
               |     90.00      10.00 |    100.00 
    -----------+----------------------+----------
             4 |         9          9 |        18 
               |     50.00      50.00 |    100.00 
    -----------+----------------------+----------
             5 |         2          9 |        11 
               |     18.18      81.82 |    100.00 
    -----------+----------------------+----------
         Total |        48         21 |        69 
               |     69.57      30.43 |    100.00 
    
    . egen wanted = mean(100 * (foreign == 1)) if inlist(foreign, 0, 1), by(rep78)
    
    . replace wanted = 100 - wanted if foreign == 0
    (43 real changes made)
    
    . table rep78 foreign, c(freq mean wanted) format(%2.0f)
    
    ------------------------------
    Repair    |
    Record    |      Car type     
    1978      | Domestic   Foreign
    ----------+-------------------
            1 |        2          
              |      100          
              | 
            2 |        8          
              |      100          
              | 
            3 |       27         3
              |       90        10
              | 
            4 |        9         9
              |       50        50
              | 
            5 |        2         9
              |       18        82
    ------------------------------
    Now I show how the same idea works with your example data. I have exchanged rows and columns in the table. Chopping "Nottingham" and "Sunderland" does not look good.

    Code:
    . clear 
    
    . input str5 id byte(disp opcl lat centre)
    
                id      disp      opcl       lat    centre
      1. "NG1" 2 2 1 1
      2. "NG2" 2 2 1 1
      3. "NG3" 2 2 2 1
      4. "NG4" 1 2 2 1
      5. "NG5" 2 2 1 1
      6. "NG6" 2 2 1 1
      7. "NG7" 2 2 1 1
      8. "NG8" 2 2 1 1
      9. "NG9" 2 2 1 1
     10. "NG10" 2 2 1 1
     11. "NGT11" 3 2 1 1
     12. "NG12" 2 2 2 1
     13. "NG13" 2 2 1 1
     14. "NG14" 3 2 1 1
     15. "NG15" 2 2 2 1
     16. "NG16" 2 2 2 1
     17. "NG17" 1 2 2 1
     18. "NG18" 3 1 1 1
     19. "NG19" 2 2 2 1
     20. "NG20" 2 2 1 1
     21. "NG21" 2 1 2 1
     22. "NG22" 1 2 1 1
     23. "NG23" 2 2 1 1
     24. "NG24" 2 2 1 1
     25. "NG25" 2 2 2 1
     26. "NG26" 1 2 2 1
     27. "NG27" 2 2 2 1
     28. "NG28" 2 2 1 1
     29. "NG29" 2 2 1 1
     30. "NG30" 1 2 2 1
     31. "NG31" 2 2 1 1
     32. "NG32" 1 2 2 1
     33. "NG33" 2 2 2 1
     34. "NG35" 2 2 2 1
     35. "NG36" 2 2 1 1
     36. "NG37" 1 2 1 1
     37. "NG38" 2 2 1 1
     38. "NG39" 2 2 1 1
     39. "NG40" 2 2 1 1
     40. "NG41" 1 2 2 1
     41. "NG42" 2 2 1 1
     42. "NG43" 2 2 1 1
     43. "NG44" 2 2 1 1
     44. "NG45" 1 2 1 1
     45. "NG46" 1 2 2 1
     46. "NG47" 2 2 1 1
     47. "NG48" 2 2 1 1
     48. "NG49" 2 2 2 1
     49. "NG50" 2 2 2 1
     50. "NG51" 2 2 2 1
     51. "NG52" 2 2 1 1
     52. "NG53" 2 2 1 1
     53. "NG54" 2 2 1 1
     54. "NG55" 2 2 2 1
     55. "NG56" 2 2 2 1
     56. "NG57" 2 2 1 1
     57. "NG58" 2 2 1 1
     58. "NG59" 3 2 1 1
     59. "NG60" 1 2 1 1
     60. "NG61" 2 2 1 1
     61. "NG62" 2 2 2 1
     62. "NG63" 2 2 2 1
     63. "NG64" 2 2 1 1
     64. "NG65" 2 2 1 1
     65. "NG66" 1 2 1 1
     66. "NG67" 1 2 1 1
     67. "NG68" 1 2 1 1
     68. "NG69" 2 2 2 1
     69. "NG70" 2 2 1 1
     70. "NG71" 2 2 1 1
     71. "NG72" 2 2 1 1
     72. "NG73" 1 2 1 1
     73. "NG74" 2 2 1 1
     74. "NG75" 2 2 1 1
     75. "NG76" 2 2 1 1
     76. "NG77" 1 2 1 1
     77. "NG78" 1 2 2 1
     78. "NG79" 2 2 1 1
     79. "NG80" 2 2 1 1
     80. "NG81" 2 2 1 1
     81. "NG82" 2 2 2 1
     82. "NG83" 1 2 2 1
     83. "NG84" 2 2 1 1
     84. "NG85" 2 2 1 1
     85. "NG86" 3 2 1 1
     86. "NG87" 2 2 1 1
     87. "NG88" 2 2 1 1
     88. "NG89" 1 2 1 1
     89. "NG90" 1 2 2 1
     90. "NG91" 1 2 2 1
     91. "NG92" 2 2 1 1
     92. "NG93" 2 2 1 1
     93. "NG94" 2 2 2 1
     94. "NG95" 2 2 1 1
     95. "NG96" 2 2 2 1
     96. "NG97" 1 2 1 1
     97. "NG98" 1 2 2 1
     98. "NG99" 2 2 1 1
     99. "NG100" 1 2 2 1
    100. "NG101" 2 2 1 1
    101. end
    
    . label values disp disp
    
    . label def disp 1 "undisplaced", modify
    
    . label def disp 2 "some", modify
    
    . label def disp 3 "marked", modify
    
    . label values opcl opcl
    
    . label def opcl 1 "open", modify
    
    . label def opcl 2 "closed", modify
    
    . label values lat lat
    
    . label def lat 1 "right", modify
    
    . label def lat 2 "left", modify
    
    . label values centre centre
    
    . label def centre 1 "Nottingham", modify
    
    . 
    . egen wanted = mean(100 * (lat == 2)) if inlist(lat, 1, 2), by(centre)
    
    . replace wanted = 100 - wanted if lat == 1 
    (66 real changes made)
    
    . table centre lat, c(freq mean wanted) format(%2.0f)
    
    -------------------------
               |     lat     
        centre | right   left
    -----------+-------------
    Nottingham |    66     34
               |    66     34
    -------------------------
    
    .

    Comment


    • #3
      You may wish to read this thread.

      P.s. Crossed with Nick's insightful reply.
      Best regards,

      Marcos

      Comment

      Working...
      X