Announcement

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

  • keep if(varname) ==r(max)

    Can someone explain the last two commands: I understand that r(max) or r(min) is to be used after the summarize command, but what operation is being performed here?

    bysort id: gen subid = _n
    order id subid
    bysort id: egen count_event_days = count(event_window_day)
    bysort id: gen max_days_in_window = count_event_days if subid==1
    tab max_days_in_window
    quietly sum count_event_days
    keep if count_event_days==r(max)


  • #2
    A data example would help. It seems that you are using someone else's code and trying to understand it. Fair enough, but code without example or context can be hard to follow, like a paragraph plucked from the middle of a novel.

    Can you give a source? You are calculating the maximum of a variable count_event_days and then keeping only observations with values of count_event_days equal to the maximum.

    My guess is that the code can be rewritten.

    Code:
    bysort id: egen count_event_days = count(event_window_day)
    egen tag = tag(id) 
    tab count_event_days if tag 
    sum count_event_days, meanonly 
    keep if count_event_days == r(max)
    Code:
    
    


    event_window_day would seem to be an indicator variable with values 1 and missing; otherwise the code would be puzzling.

    Comment

    Working...
    X