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

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

 From prakash singh To statalist@hsphsun2.harvard.edu Subject Re: st: RE: generating variable from combination of three categorical variable Date Thu, 19 May 2011 10:31:34 +0530

```Thanks Nick and Maria

I was able to do till this point but got stuck when we have the situation
see the cell arrangement for better understanding my problem
var1	var2	var3	var4
1	2	1	2 (correct 3)
2	2	1	2 (correct 3)
1	1	2	2 (correct 3)
2	1	2	2 (correct 3)
0	2	2	3 (correct 2)
2	2	2	3 (correct 2)

I have modified the commands also a bit
gen var4 = max( var1, var2, var3)
replace var4 = 3 if ( var1+ var2+ var3) >= 3 & var4!=1
replace var4= max(var1, var2, var3) if var1==var2 & var1~=var3 |
var1==var3 & var1~=var2
but this is also not working perfect.

Prakash

On 5/18/11, Nick Cox <njcoxstata@gmail.com> wrote:
> Good catch.
>
> Nick
>
> On 18 May 2011, at 14:17, Maria Fleischmann <maria.s.fleischmann@gmail.com
>  > wrote:
>
>> Shouldn't it be:
>>
>> gen var4 = max(var1, var2, var3)
>> replace var4 = 3 if (var1 + var2 + var3) == 3 & var4!=1
>>
>> otherwise you subsitute var4=3 if var1, var2, and var3 are all equal
>> to '1'
>>
>>
>> Maria
>>
>> On 18 May 2011 14:37, Nick Cox <n.j.cox@durham.ac.uk> wrote:
>>>
>>> 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/
>>
>> *
>> *   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/
> *
> *   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/
>
*
*   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/
```