Announcement

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

  • Using egen with a conditional

    Hi guys,

    Still a STATA rookie. I'm trying to figure out how to store a conditional median as a variable.

    So I can get egen HL_cutoff = pctile( Day_max ), p(50) to work. But the trouble is I need it to be conditional - I need HL_cutoff to be the median of Day_max among people who have another variable, V1=0.

    I've tried things like"
    egen HL_cutoff = pctile( Day_max ), p(50) if V1==0
    egen if V1==0 HL_cutoff = pctile( Day_max ), p(50)

    EDIT: to be clear, I just what the computation of the median to be conditional. The variable should still apply to everybody.

    What is the easiest way to do this?

    Thanks!
    Last edited by Darren Anderson; 31 Jul 2017, 11:40.

  • #2
    Code:
    clear
    input y v1
    1 0
    2 0
    3 0
    4 0
    5 0 
    6 1
    end
    
    egen m1 = pctile(cond(v1 == 0, y, .))
    See also Cox N., Speaking Stata: Compared with . . . , The Stata Journal (2011) 11, Number 2, pp. 305–314

    Comment


    • #3
      Thanks to Andrea for the clear example and the publicity. Let's be clear that Andrea's code puts the median in all observations while

      Code:
       
       egen m1 = pctile(y) if v1 == 0
      would put it only in those observations specified.

      Comment

      Working...
      X