Announcement

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

  • Creating aggregate variables

    Hi, This is my first time posting here, so I appreciate your kindness>
    I have a dataset with observations at individual-level, and I want to create a new variable (% of a therapy) at a center-level.
    I have the therapy variable as (0-1) and the variable that identifies the center-level I want to aggregate in as a string variable.
    Please help, I have tried bysort, egen, count with no satisfactory results.

  • #2
    I think you will need to give a data example if you don't get a better answer than this.

    I can guess at one or more individuals attending one or more centers. Beyond that I can't follow what you want: the % of individuals attending each center who receive a certain therapy? The % of individuals receiving a therapy attending a certain center? Why should string variables be involved?

    https://www.statalist.org/forums/help#stata

    should help you.

    Comment


    • #3
      I see what you mean, Let me explain myself:
      I have TherapyA as 0-1, and I want to identify at the center-level, the percentage of recipients at each center that received TherapyA.
      Each center is labeled and centers exclude each other (one or more pts attending just one center)
      I will check on you recommendation...

      Comment


      • #4
        So, im using STATA 15.1 with this command:
        bysort ctr_code: egen num_induct= count if inducTherapy=="1"
        gen Percent_induc=100*num_induct/Total_tx, by(ctr_code)


        This is what I tried so far..

        Comment


        • #5
          Code:
          bysort ctr_code: egen num_induct= count if inducTherapy=="1"
          
          gen Percent_induc=100*num_induct/Total_tx, by(ctr_code)
          Both commands are illegal, but I think I can guess what you want.

          Code:
          bysort ctr_code: egen num_induct= total(inducTherapy=="1")
          
          by ctr_code : gen percent_induct = 100 * num_induct/_N
          And another way to do it:

          Code:
          bysort ctr_code: egen num_induct= mean(100 * (inducTherapy=="1"))
          Last edited by Nick Cox; 25 Nov 2019, 14:11.

          Comment


          • #6
            Thank you very much!

            Comment

            Working...
            X