Announcement

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

  • egen help

    I am trying to find the commands and code that will allow me to generate a new variable. This is a simplified table example. Each row is a unique individual patient labeled patient_ID. The treating hospital (hospital_ID is not unique). In this example, there are 3 types of treatment (1=resection, 2= ablation, 3=transplantation). I am trying to make a new variable to code an individual hospital as a transplant center. In this example, only hospital 2 and hospital 4 should have a value of 1 for TXP_center.

    I have some experience with egen, but I am currently stuck.

    thanks
    patient_ID hospital_ID surg_type TXP_center
    1 1 1 0
    2 1 2 0
    3 2 3 1
    4 2 1 1
    5 3 1 0
    6 3 2 0
    7 4 3 1
    8 4 1 1

  • #2
    You may try something like:

    Code:
    egen TXP_center = max(surg_type ==3), by(hospital_ID)
    Note: not tested, because I'm out of "my" Stata at this very moment.
    Last edited by Marcos Almeida; 07 May 2018, 09:18.
    Best regards,

    Marcos

    Comment


    • #3
      CWray:
      Marcos code works perfectly for me.
      I was thinking about something less efficient:
      Code:
      . egen TXP_center_2=tag( hospital_ID) if surg_type==3
      
      . bysort hospital_ID: replace TXP_center_2= TXP_center_2[_n-1] if TXP_center_2[_n-1]==1
      
      . drop TXP_center
      
      . rename TXP_center_2 TXP_center
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        Wow. That totally worked! Thanks Marcos.

        I spent hours on Saturday trying to figure this out.

        Comment


        • #5
          CWray Great that it worked for you. Please prefer to use name and family name, as recommended in the FAQ. You may re-register by clicking on the "contact us" button. Thanks.

          Carlo Lazzaro: the code I provided concerns being constantly "on the shoulders of giants". This FAQ, written by Nick Cox , was quite inspirational, to say the least.
          Last edited by Marcos Almeida; 07 May 2018, 09:42.
          Best regards,

          Marcos

          Comment


          • #6
            Marcos:
            I see your point.
            I keep trying to be piggybacked onto Stata giants' shoulders, too!!
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment

            Working...
            X