Announcement

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

  • greater or equal not working with 2 decimals

    Hello!

    I am trying to create a categorical variable (Y/N) when another variable measuring proportions is greater than or equal to 0.96, using operator "=>". However, Stata only marks proportions that are greater than 0.96, skipping those that are equal to 0.96:
    clear
    set obs 1000
    set seed 2
    * cutoff with 2 decimals:
    gen rand_2decimals = runiformint(0, 100)/100
    gen atorabove96 = "Y" if rand_2decimals >=.96
    sort rand_2decimals
    * Notice 8 observations with value 0.96 that were not marked "Y".


    *I also found that this problem does not appear when using more decimals (using 7 decimals below):

    * cutoff with more decimals:
    gen rand_moredec = runiform()
    gen atorabove96_more = "Y" if rand_moredec >=.96
    sort rand_moredec
    * I can even create another categorical variable from a cutoff to 7 decimals, and Stata will correctly include the case with a value exactly equal to the cutoff:
    gen cutoff="y" if rand_moredec>= .9609721

    Can someone explain why Stata does not recognize >=0.96, or tell me where my error is?

  • #2
    Originally posted by Emilio Herrerias View Post
    Can someone explain why Stata does not recognize >=0.96, or tell me where my error is?
    Use a display format that shows more decimals, say,
    Code:
    format rand_2decimals %010.8f
    and you'll see that Stata is correct.

    Comment


    • #3
      Oh, I forgot to mention: if you're wondering about why you didn't get exactly 0.96, see help precision.

      Comment

      Working...
      X