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

RE: st: Code to generate dummy variable from several categorical variables?

 From Nick Cox To "'statalist@hsphsun2.harvard.edu'" Subject RE: st: Code to generate dummy variable from several categorical variables? Date Tue, 17 Jan 2012 20:12:58 +0000

Comments below. I think all the issues raised previously still bite.

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

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of DEBORAH L. HUANG
Sent: 17 January 2012 19:40
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: Code to generate dummy variable from several categorical variables?

Basically what I'm hoping to do is "collapse" the outcome variables A, B and C (all binary) into the new outcome indicator variable abnlX for ANOVA (e.g., comparison mean age across indicators, among other continuous demographic variables).

The new outcome variable abnlX would have 3 indicators (my mistake in the earlier message). As an indicator variable abnlX would be defined as follows:

abnlX indicator #1 =0 if A is 0 or missing, B is 0/1/missing, C is 0/1/missing; =1 if A is 1, B is 0/1/missing, C is 0/1/missing

abnlX indicator #2 =0 if B is 0 or missing, A is 0/1/missing, C is 0/1/missing; =1 if B is 1, A is 0/1/missing, C is 0/1/missing

abnlX indicator #3 =0 if C is 0 or missing, A is 0/1/missing, B is 0/1/missing; =1 if C is 1, A is 0/1/missing, C is 0/1/missing

% NJC: Already answered in this thread. Please note as before this can only be 3 outcome variables (not one) which are also indicators.

% gen abnlX1 = A == 1
% gen abnlX2 = B == 1
% gen abnlX3 = C == 1

Alternately for a categorical outcome variable abnlX it would be defined as follows:
abnlX=0 if A=0 or missing & B=0 or missing & C=0 or missing
abnlX=1 if A=1 & B=0/1/missing & C=0/1/missing
abnlX=2 if B=1 & A=0/1/missing & C=0/1/missing
abnlX=3 if C=1 & A=0/1/missing & B=0/1/missing

% NJC: This is not an indicator or dummy variable, as already explained on this thread, because there are 4 possible values.
% Note that, for example, A = 1 and B = 1 and C = 1 imply 1 _and_ 2 _and_ 3, so I am completely lost here on what you want.

Thank you again to everyone for your input, and hopefully this further clarifies my question.

Deborah Huang

On Tue, Jan 17, 2012 at 7:22 AM, David Hoaglin <dchoaglin@gmail.com> wrote:

>It would help to have further clarification.
>
>As Nick pointed out, an indicator variable (aka dummy variable) has two (non-missing) >values: 0 and 1.  Please explain what you mean by "a dummy variable with 4 indicators" and >then give an explicit definition of the desired "dummy variable" in terms of A, B, and C.
>
>If you actually want a categorical variable with 4 categories (which would necessarily be >mutually exclusive), please define those categories in terms of A, B, and C.
>
>Your explanation of the "dummy variable" abnlX lists three indicator variables.  If you >intend abnlX to be a categorical variable, those three indicators are not mutually exclusive.
>
>It would help if you described the role that the new variable will play in an analysis.  >Some regression models, for example, could include the binary variables A, B, and C as they >stand; they would not need to be mutually exclusive.
>
>BTW, three binary variables yield 8 possible combinations.  The one not in your list is A=1, >B=0, C=1.  Why is it necessary to re-categorize this subject and subjects #2, #3, and #5?
>

*
*   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/