Announcement

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

  • Help

    Hi, I need help with solving this problem of duplicates, drop ,etc.
    I have the following data:

    I would like to keep the patient with headinj (inc_key A & D) and drop those without (inc_key B & C)
    But I also want to keep the remaining body part, duplicates inc_key of A & D
    Another word, I want to keep all of the inc_key A & D
    How do I write a command for this?
    Thank you in advance
    Narong

    inc_key organ severity headinj
    A head 3 yes
    A chest 3 .
    A abdomen 2 .
    B chest 3 .
    B abdomen 2 .
    C abdomen 2 .
    D head 3 yes
    D chest 3 ,
    D abdomen 2 .

  • #2
    Narong:
    welcome to this forum.
    Do you mean something along the following lines?
    Code:
    . bysort inc_key: replace headinj="headpluselse" if headinj[1]=="yes" & headinj=="."
    
    
    . drop if headinj=="."
    
    
    . list
    
         +---------------------------------------------+
         | inc_key     organ   severity        headinj |
         |---------------------------------------------|
      1. |       A      head          3            yes |
      2. |       A     chest          3   headpluselse |
      3. |       A   abdomen          2   headpluselse |
      4. |       D      head          3            yes |
      5. |       D     chest          3   headpluselse |
         |---------------------------------------------|
      6. |       D   abdomen          2   headpluselse |
         +---------------------------------------------+
    
    .
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Here's another approach that does not depend on the head injury row being listed first for each patient.

      Code:
      * Read in the sample data
      clear
      input str1 inc_key str15 organ severity str3 headinj
      A head 3 yes
      A chest 3 .
      A abdomen 2 .
      B chest 3 .
      B abdomen 2 .
      C abdomen 2 .
      D head 3 yes
      D chest 3 ,
      D abdomen 2 .
      end
      * Rename headinj to head
      rename headinj head
      * Generate a numeric indicator variable for head injury
      * so that I can use the egen max() function later
      generate byte headinj = head=="yes"
      bysort inc_key: egen wanted = max(headinj)
      list, sepby(inc_key)
      keep if wanted
      list, sepby(inc_key)
      Output from the final list command:
      Code:
      . list, sepby(inc_key)
      
           +--------------------------------------------------------+
           | inc_key     organ   severity   head   headinj   wanted |
           |--------------------------------------------------------|
        1. |       A      head          3    yes         1        1 |
        2. |       A     chest          3      .         0        1 |
        3. |       A   abdomen          2      .         0        1 |
           |--------------------------------------------------------|
        4. |       D      head          3    yes         1        1 |
        5. |       D     chest          3      ,         0        1 |
        6. |       D   abdomen          2      .         0        1 |
           +--------------------------------------------------------+


      PS- Welcome to the forum Narong Kul, but please take a look at the FAQ for tips on how to write questions in a way that makes it easier for forum members to help you!

      --
      Bruce Weaver
      Email: [email protected]
      Version: Stata/MP 18.5 (Windows)

      Comment

      Working...
      X