Announcement

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

  • How to generate a categorical variable for all combinations of a list?

    Hello,

    I previously received help generating an indicator variable that would take the value of all possible combinations of a list. In order that the name of the indicator would correspond with what it represented, I used this command:

    Code:
    gen byte f  = sex==2
    gen byte n  = bpl<=99
    gen byte w  = race==1 & !inlist(hispan,1,2,3,4)
    gen byte y  = age<=25
    gen byte s  = inlist(marst,3,4,5,6)
      
    forval f=0/1 {
    forval n=0/1 {
    forval y=0/1 {
    forval w=0/1 {
    forval s=0/1 {
      gen cat_f`f'_n`n'_y`y'_w`w'_s`s'= ///
      (f==`f')*(n==`n')*(y==`y')*(w==`w')*(s==`s')
    }
    }
    }
    }
    }
    Where f = female, n = native, w = white, y = age<=25, and s = single. Now, instead of 31 indicators, I'm trying to generate one categorical variable that will take the corresponding value for all observations. My intutition is to do the following:

    Code:
    generate ct = 1
    and then use the egen command, but having trouble finding out how I would correctly do this.

    Thank you.

  • #2
    Maybe - egen - with - group() - would be helpful.
    Best regards,

    Marcos

    Comment


    • #3
      I think this is unnecessarily complicated. See -help egen- and look for the -group()- function.

      Code:
      egen ct = group(f n y w s)
      Added: Crossed with #2 which gives the same advice.

      Comment


      • #4
        Thank you Marcos and Clyde. You were both right.

        Comment

        Working...
        X