Announcement

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

  • Rounding to the left of the decimal point using display formats only

    Let's say I have some statistics I would like to calculate using the table command (or tabstat for that matter) and I would like to retain the integrity of the underlying data but round the results that get displayed to, say, the nearest hundred or thousand. Is that possible? Looking through all of the documentation on display formats, I think it is not. A data example:

    Code:
    sysuse census.dta, clear
    table (region) (var), stat(sum pop)
    Displays:

    Code:
    ----------------------------
                  |   Population
    --------------+-------------
    Census region |             
      NE          |   49,135,283
      N Cntrl     |   58,865,670
      South       |   74,734,029
      West        |   43,172,490
      Total       |  225,907,472
    ----------------------------
    When I would like it to display:

    Code:
    ----------------------------
                  |   Population
    --------------+-------------
    Census region |             
      NE          |   49,135,000
      N Cntrl     |   58,866,000
      South       |   74,734,000
      West        |   43,172,000
      Total       |  225,907,000
    ----------------------------

  • #2
    You can't do that with display formats as the rounded values as you want (e.g., rounding to the nearest thousand) is typically a fundamentally different number from the raw value. You could first generate, then summarize, a value of population divided by 1000 (or some suitable unit).

    Code:
    gen pop1k = pop / 1000

    Comment


    • #3
      I don't know of any options or settings within the -table- command that will do this. But it is simple enough to make a new variable that is the original one, rounded, and use the new variable in the table command.
      Code:
      . sysuse census.dta, clear
      (1980 Census data by state)
      
      .
      . gen double pop2 = round(pop, 1000)
      
      . table (region) (var), stat(sum pop2)
      
      ----------------------------
                    |         pop2
      --------------+-------------
      Census region |            
        NE          |   49,136,000
        N Cntrl     |   58,868,000
        South       |   74,734,000
        West        |   43,173,000
        Total       |  225,911,000
      ----------------------------

      Comment


      • #4
        Clyde's answer, while simple, will produce incorrect results because he rounds population before calculating the statistics. For example, his output shows the total population to be 225,911,000 while the correct figure is 225,907,000 (rounded to the nearest thousand).

        The solution is only slightly convoluted. We just need to write a command that calculates the sum rounded to the nearest 1000:

        Code:
        clear all
        sysuse census
        
        program mean1000, rclass
            summ `0', mean
            return scalar Value = 1000*round(r(sum)/1000)
        end
        
        table (region) (result), command(mean1000 pop) nformat(%12.0fc)
        
        ----------------------------
                      |        Value
        --------------+-------------
        Census region |             
          NE          |   49,135,000
          N Cntrl     |   58,866,000
          South       |   74,734,000
          West        |   43,172,000
          Total       |  225,907,000
        ----------------------------
        If you also want the flexibility to round to the nearest 100 or million you could write your command to accept an option specifying the amount of precision desired.

        Comment


        • #5
          This is the only reasonable and straightforward solution I can see if you insist on using -table-.

          Code:
          gen pop1k = pop / 1000
          table (region) (var), stat(sum pop) stat(sum pop1k) nformat(%12.0fc)
          Result

          [/code]
          --------------------------------------
          | Population pop1k
          --------------+-----------------------
          Census region |
          NE | 49,135,283 49,135
          N Cntrl | 58,865,670 58,866
          South | 74,734,029 74,734
          West | 43,172,490 43,172
          Total | 225,907,472 225,907
          --------------------------------------
          [/code]

          If you insist on the extra zeros, then you have to find some ad hoc workaround as above, or build your table "manually". YMMV, especially if you need to display more than just simple sum as summary statistics.

          Comment


          • #6
            Yes, sorry. Brian Poi is correct -- my solution rounds the numbers to be added, not the sum, and gives incorrect results.

            Comment


            • #7
              Thanks all - this is all super helpful.

              Comment

              Working...
              X