Announcement

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

  • Creating categorical group variable with one group including the other

    Dear all,

    I have two groups: group 1: 2,109 observations; group 2: 1,615 observations, which is indicated by two binary variables: group1 (1 if part of group1, 0 if not), group2 (1 if part of group2, 0 if not).

    However, group 1 actually includes group 2.

    I want to create a variable that indicates 1 if the observation is part of group 1 (2,109); and a 2 if an observation is part of group 2 (identifying 1,615 observations)

    However, the tricky part is, I do not want to override the 1 values with the 2 values as this would happen if I would use:

    Code:
    gen total_group=.
    replace total_group=1 if group1==1
    replace total_group=2 if group2==1
    Is there a way to achieve this?

    Thank you all in advance!

  • #2
    That seems logically impossible. Say we have a person who is in group 1 and in group 2. For your requirements to work, your variable total_group should be 1 and 2, but a single variable cannot have two values at the same time for a single observation. What you can do is make a four category variable: 1 only group 1, 2 only group 2, 3 group 1 and 2, 4 not group 1 and not group 2.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Hi Maarten,
      Thank you for your reply. I see your point. Could you elaborate further on how to create such a variable?
      Many thanks!

      Comment


      • #4
        Code:
        gen byte total_group: total_group_lb = .
        replace total_group = 1 if group1 == 1 & group2 == 0
        replace total_group = 2 if group2 == 1 & group1 == 0
        replace total_group = 3 if group1 == 1 & group2 == 1
        replace total_group = 4 if group1 == 0 & group2 == 0
        
        label define total_group_lb 1 "only group 1" ///
                                    2 "only group 2" ///
                                    3 "group 1 and 2" ///
                                    4 "neither group 1 nor 2"
                                    
        label variable total_group "Grouping 1 and 2 combined"
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Thanks a lot Maarten, you made my day!

          Comment


          • #6
            See also https://www.stata-journal.com/articl...article=dm0034

            Comment

            Working...
            X