Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: RE: generating variable from combination of three categorical variable

 From Nick Cox To "'statalist@hsphsun2.harvard.edu'" Subject st: RE: generating variable from combination of three categorical variable Date Wed, 18 May 2011 13:37:04 +0100

```You have six rules, which I think can be collapsed to 4 in the first instance.

(a) var1==0 & var2==0 & var3==0,
(b) var4=1 if var1==1 & var2==1 & var3==1
(c) var4=2 if var1==2 & var2==2 & var3==2
(d) var4=1 if var1|var2|var3 is having 0 and 1 only
(e) var4=2 if var1|var2|var3 is having 0 and 2 only
(f) var4 is 3 if var1|var2|var3 is 0,1 and 2

gen var4 = 0 if max(var1, var2, var3) == 0            (a)
replace var4 = 1 if max(var1, var2, var3) == 1        (b), (d)
replace var4 = 2 if max(var1, var2, var3) == 2        (c), (e)
replace var3 = 3 if (var1 + var1 + var2) == 3         (f)

Now it is evident that you can simplify

gen var4 = max(var1, var2, var3)
replace var3 = 3 if (var1 + var1 + var2) == 3

Nick
n.j.cox@durham.ac.uk

prakash singh

In my survey data, I have three categorical variable with 0, 1 and 2 as
three category.
Now I want to make a variable using combination of these three
variables.

The combination is such that generated variable var4 == 0 if
var1==0 & var2==0 & var3==0,
var4=1 if var1==1 & var2==1 & var3==1,
var4=2 if var1==2 & var2==2 & var3==2,
var4=1 if var1|var2|var3 is having 0 and 1 only
var4=2 if var1|var2|var3 is having 0 and 2 only
var4 is 3 if var1|var2|var3 is 0,1 and 2

for exmple (see below)

var1 var2 var3 var4
0     0     0      0
1    1     1       1
2    2      2      2
0    1     2       3
0    1      1      1
1    2      0      3

I am using stata10.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```