Announcement

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

  • Conditioning on specific values of a variable

    Dear all,

    I have a variable, call it e, containing values 0.05(0.05)0.25; I filled this variable with these values manually, meaning I wanted that it contains exactly these values. Now, when I do something like this, for example:
    Code:
    forval e = 0.05(0.05)0.25 {
         sum some_variable if e == `e'
    }
    or
    Code:
    forval e = 0.05(0.05)0.25 {
         sum some_variable if e == float(`e')
    }
    in either case, it sometimes works and sometimes does not work. By "it does not work" I mean observations satisfying e == `e' or e == float(`e') are not found for some values of `e'. And by "sometimes" I mean now it works, but when I exit Stata and start it again, it may not work.

    But I know that there are observations e=0.05, e=0.10, e=0.15, e=0.20, and e=0.25 because I entered them. I know it's something about precision, I know that how values are displayed is not exactly how they are represented in the machine, which is the reason I tried with the float( ) function, but still I cannot solve the problem.

    Please, help!

    Best regards,
    Ivica
    // ivica_rubil //

  • #2
    The problem is here is precision and you are half-way to a solution there. See https://www.stata-journal.com/articl...article=pr0051 for a detailed discussion. A solution is to loop over integers and multiply inside the loop.

    Code:
     
     forval x = 1/5 {      sum some_variable if e == float(`x' * 0.05) }
    Of multiples of 0.01 only multiples of 0, 0.25, 0.50, 0.75 and 1 can be held exactly in binary.

    Otherwise hold 1 to 5 in memory and regard the units as twentieths (multiples of 1/20).

    Comment


    • #3
      Thanks a lot, Nick!
      // ivica_rubil //

      Comment

      Working...
      X