Announcement

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

  • Round 2 digits after decimal

    Hello,

    My data is attached as follows. I would like to round the data two digits after decimal (o.oo). Used this command to execute

    replace production_index = round (production_index, 0.1)
    round not found
    r(111);

    It shows the above message.

    Data is like this
    input str134 industries int D str12 E str38 production_index float ID byte month float qdate
    "B. (Mining And Quarrying)" 2010 "01-January" "68.90000000000001" 2 1 200
    "B. (Mining And Quarrying)" 2010 "02-February" "65" 2 2 200
    "B. (Mining And Quarrying)" 2010 "03-March" "75.3" 2 3 200
    "B. (Mining And Quarrying)" 2010 "04-April" "79.5" 2 4 201
    "B. (Mining And Quarrying)" 2010 "05-May" "81.09999999999999" 2 5 201
    "B. (Mining And Quarrying)" 2010 "06-June" "95.3" 2 6 201
    "B. (Mining And Quarrying)" 2010 "07-July" "97.5" 2 7 202
    "B. (Mining And Quarrying)" 2010 "08-August" "99.09999999999999" 2 8 202

    Any advice appreciated!

  • #2
    I think there is a typo since there should not be a space between round and the parentheses. Also, do you really need to round or do you just want to change the display format? See help format for advice.
    Best wishes

    (Stata 16.1 MP)

    Comment


    • #3
      The immediate reason for this failing is the spurious space. round() is all one name. With an extra space, Stata thinks you are referring to a variable (or scalar) called round; no such beast exists, so it bails out.

      But you don't need to do any rounding. Sure, to people reading this 68.90000000000001 "should be" 68.90.

      However, 68.90 doesn't have an exact binary equivalent and round() won't get you closer to something that doesn't exist. Consider these results in hexadecimal

      Code:
      . di %21x 68.90000000000001
      +1.139999999999aX+006
      
      . di %21x 68.90
      +1.139999999999aX+006
      The same applies to other odd-looking numbers.

      Note that you are showing a production index that is a string variable, so round() should fail for that reason.

      I've already pointed in a previous thread -- https://www.statalist.org/forums/for...quarterly-data -- that you need to destring this variable. When you have done that, by all means assign a display format

      Code:
      destring production_index, replace
      format production_index %3.2f 
      That still leaves a question of why the variable is string any way, which I can't illuminate. The answer lies in how your dataset got to be what it is, on which we have no information.
      Last edited by Nick Cox; 23 Jan 2020, 06:32.

      Comment

      Working...
      X