Announcement

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

  • Creating a variable containing number of decimal points of another variable's values

    Hi, I have a variable measuring speed. The values are double and vary in the number of decimal points. I would like to create a new variable that will count the number of decimals in the speed variable. Please, how can this be done?

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double speed
     1.55
      1.2
        2
    1.433
    2.123
     1.43
    1.158
    end

  • #2
    Well, I think what you are asking for is, in principle, impossible. But I think it is possible to come close.

    The reason I say it's impossible is that a number like 1.55 has no exact finite representation in binary (which is how Stata stores numbers). So although you think of the first value as being 1.55, it is in fact some other number that is as close as possible to 1.55 with a finite number of binary digits. The real number might actually be more like 1.49999999999999998 something like that. Stata is fairly smart about rounding out those little discrepancies when displaying results.

    The following code works for all of the numbers in your example. In principle, I think one could contrive numbers that would cause this code to give the wrong answer due to the precision issue cited above. But as long as the maximum number of decimal places you are looking for is fairly small, it won't happen. I'm assuming here that the maximum number of decimal places is 5. You can replace 5 by the actual upper bound for your problem. If it gets too large, however, as already stated, the code will fail.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double speed
     1.55
      1.2
        2
    1.433
    2.123
     1.43
    1.158
    end
    
    local multiplier 1
    gen decimal_places = .
    forvalues i = 0/5 {
        replace decimal_places = `i' if missing(decimal_places) ///
            & `multiplier'*speed == int(`multiplier'*speed)
        local multiplier = `multiplier' * 10
    }
    list, noobs clean
    Note: I'm also assuming that if you had a value, say, 3.0, that the answer you want is 0, not 1. 3.0 and 3 will have exactly the same binary representation inside Stata, so there is absolutely no way that Stata would be able to know how many decimal places you intended. What the code above is counting is the number of decimal places up to reaching the point where any additional decimal places will be 0.

    Comment


    • #3
      Modulo Clyde's caution on knowing what the current display of your numeric data actually represents, you could tackle this problem by converting to strings.

      Code:
      gen speed_str = string(speed)
      gen dp_position = strpos(speed_str,".")
      gen dp_num = cond(dp_pos >0, strlen(speed_str) - dp_pos, 0)
      list

      Comment


      • #4
        If you mean number of decimal points — as in your title — then

        Code:
        count if speed != floor(speed)

        Comment


        • #5
          Thank you for the answers! Excuse me for the mistake - it was supposed to be decimal places.

          Comment

          Working...
          X