Announcement

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

  • Splitting a variable into three different groups

    Hey everyone,

    I am trying to split a RA_reservoir into three groups based on its quartiles.

    RA_reservoir <= 31.35 = 1

    31.35 < RA_reservoir <46.6 =2

    RA_reservoir >= 46.6 =3


    I used the code:

    gen RA_reservoir3groups = 0 if (RA_reservoir<=31.35)
    replace RA_reservoir3groups =1 if (31.36<RA_reservoir<46.6)
    replace RA_reservoir3groups =2 if (RA_reservoir>=46.6)

    However, when I do this it gives all observations a 1, removing all the 0's.So this code is not working for me.

    Thank you



  • #2
    your first replace statement is not what you want; see
    Code:
    h inrange

    Comment


    • #3
      Rich Goldstein gives you the fix for your code. I'll explain to you why it didn't work the way you expected. You intended the expression -31.36<RA_reservoir<46.6- to mean -31.36<RA_reservoir & RA_reservoir<46.6-. That is common usage in mathematics, but it is not consistent with Stata syntax.

      Stata parses -31.36<RA_reservoir<46.6- as -(31.36<RA_reservoir) < 46.6-. What does that mean? In Stata all logical expresions are evaluated numerically as either 0 if false or 1 if true. So the value of -(31.36 < RA _reservoir)- will always be either 0 or 1, both of which are less than 46.6. So -31.36<RA_reservoir<46.6- is always true. And that is why all the zeroes got replaced by ones in your new variable.

      Comment

      Working...
      X