Announcement

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

  • Issue when rounding

    I have an issue when rounding a variable in STATA
    I use the following STATA function

    gen Rounded_ Quantity = round(Quantity, 1.0)

    I am presenting in the table below how it looks.

    Sometimes when the value of the variable is not a decimal number (i.e. when it is an integer), the rounded value reduces by 1 or increases by 1 ( as shown in the results of ID 1 and ID 2). I don’t get the reason. I think in both cases, the values of Rounded_ Quantity is supposed to be the same as the values of Quantity. I am wondering why it is not the same.

    However, for some integers, it still works perfectly. The value of the Rounded_ Quantity is the same as the values of Quantity as shown in the result for ID 3.

    When the value of the variable is decimal, it works perfectly, as shown in the results for IDs 4 and 5.
    ID Quantity Rounded_ Quantity= round(Quantity, 1.0)
    1 18169497 18169496
    2 17356187 17356188
    3 22467414 22467414
    4 6472872.3 6472872
    5 5513729.8 5513730
    Would you please suggest to me any clue why the rounded value of 1 and 2 changed this way? Would you offer me any solution for this, please?

    Thank you, and looking forward to hearing from you,



  • #2
    The issue isn't rounding. Or rather it is one of rounding but round() is not to blame.

    It's that you need a different variable type to hold integers that large exactly. By default the new variable will be a float, but a long or double would make more sense.


    Code:
    . gen whatever = 18169497
    
    . di whatever[1]
    18169496
    
    . gen long whatevernext = 18169497
    
    . di whatevernext[1]
    18169497
    
    . gen double whateverelse = 18169497
    
    . di whateverelse[1]
    18169497

    Comment


    • #3
      For reference, here are the limits on storage of decimal integers with full accuracy in the various numeric storage types. The fixed-point variables lose the 27 largest positive values to missing value codes; the similar loss for floating point variables occurs only for the largest exponent, so it doesn't affect the much smaller integer values.
      byte - 7 bits -127 100
      int - 15 bits -32,767 32,740
      long - 31 bits -2,147,483,647 2,147,483,620
      float - 24 bits -16,777,216 16,777,216
      double - 53 bits -9,007,199,254,740,992 9,007,199,254,740,992

      Comment


      • #4
        Thank you very much indeed, Nick Cox!! It worked well with the long and double format.

        Comment


        • #5
          Good, but do note that long and double are not in Stata terms formats. They are variable or storage types.

          Comment


          • #6
            Yes, they are storage types. Thank you very much for the correction. Best regards!!

            Comment

            Working...
            X