Announcement

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

  • Looping through string variable by groups

    Hello,

    I need to determine cases where two surgeons worked in the same room on a specific day. For example, in the data below, you'll see that both Dr. Devin and Dr. Monchak worked in Room 6 on date 22831. However, Dr. DeJean worked only in room 2 on that day. I need to create two seperate datasets. One includes only cases like Dr. DeJean where he worked in OR 2 only. The other dataset would include all of Dr. Monchacks (even when he worked in room5) and all of Dr. Devin''s cases - all cases where two surgeons shared a room. I grouped the date of service and room together. I was hoping I could use something like gen tagger=1 if Group==Group[_n-1] & PhysName!=PhysName[_n-1]. I would then use bysort Group: egen tokeep=max(tagger==1). Unfortunately, I seem to be missing cases and I relaize it's not the best way to go about this. Loops have always confused me - even after wathcing youtube videos and reviewing the stata manual. Any help would be amazing.

    Thank you!


    input float Group int DOS str24 PhysName str16 Room
    5 22831 "DeJean John" "OR 2"
    5 22831 "DeJean John" "OR 2"
    5 22831 "DeJean John" "OR 2"
    5 22831 "DeJean John" "OR 2"
    5 22831 "DeJean John" "OR 2"
    5 22831 "DeJean John" "OR 2"
    6 22831 "Monchack, Kevin P" "OR 5"
    6 22831 "Monchack, Kevin P" "OR 5"
    6 22831 "Monchack, Kevin P" "OR 5"
    6 22831 "Monchack, Kevin P" "OR 5"
    7 22831 "Monchack, Kevin P" "OR 6"
    7 22831 "Monchack, Kevin P" "OR 6"
    7 22831 "Monchack, Kevin P" "OR 6"
    7 22831 "Devin, Kate D" "OR 6"

  • #2
    This does not require any loops.

    Code:
    by Group Room DOS (PhysName), sort: gen n_doc = sum(PhysName != PhysName[_n-1])
    by Group Room DOS: gen multiple_use = n_doc[_N] > 1
    The variable multiple_use will be 1 in all observations of any group/date of service/room combination where more than one doctor worked. It will be 0 when only one doctor in the group used that room that date. You can then allocate those to separate data sets if you wish.

    Comment


    • #3
      Thanks, Clyde!

      Comment

      Working...
      X