Announcement

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

  • continuous variable to categorical one.

    Dear All, Is there a more concise way to do the following.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(Y p10 p20 p40 p60 p80 p90)
    200000 26000 36000 50000 80000 120000 200000
    20000 26000 36000 50000 80000 120000 200000
    40000 26000 36000 50000 80000 120000 200000
    30000 26000 36000 50000 80000 120000 200000
    100000 26000 36000 50000 80000 120000 200000
    300000 26000 36000 50000 80000 120000 200000
    96000 26000 36000 50000 80000 120000 200000
    16000 26000 36000 50000 80000 120000 200000
    50000 26000 36000 50000 80000 120000 200000
    60000 26000 36000 50000 80000 120000 200000
    50000 26000 36000 50000 80000 120000 200000
    100000 26000 36000 50000 80000 120000 200000
    50000 26000 36000 50000 80000 120000 200000
    40000 26000 36000 50000 80000 120000 200000
    90000 26000 36000 50000 80000 120000 200000
    end
    
    gen Y1=.
    replace Y1=1  if  Y <= p10
    replace Y1=2  if  p10 < Y & Y <= p20
    replace Y1=3  if  p20 < Y & Y <= p40
    replace Y1=4  if  p40 < Y & Y <= p60
    replace Y1=5  if  p60 < Y & Y <= p80
    replace Y1=6  if  p80 < Y & Y < =p90
    replace Y1=7  if  Y > p90
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    Code:
    gen wanted=1
    foreach var of varlist p*{
        replace wanted = wanted +1 if `var'<Y
    }

    Comment


    • #3
      Dear Andrew, Many thanks for this helpful suggestion.
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment


      • #4
        Here's another way to do it:


        Code:
        gen wanted1 = 1
        
        foreach v in 10 20 40 60 80 90 {
            replace wanted1 = wanted1 + 1 if Y > p`v'
        }

        Comment


        • #5
          Dear Nick, This is also helpful.

          Ho-Chuan (River) Huang
          Stata 19.0, MP(4)

          Comment


          • #6
            For this specific issue, manual approach does not look bad.
            Code:
            gen W = 1 + (Y>p10) + (Y>p20) + (Y>p40) + (Y>p60) + (Y>p80) + (Y>p90)

            Comment


            • #7
              Dear Romalpa, Thanks for offering this helpful ("manual") method.
              Ho-Chuan (River) Huang
              Stata 19.0, MP(4)

              Comment

              Working...
              X