Bookmark and Share

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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From   "DEBORAH L. HUANG" <[email protected]>
To   [email protected]
Subject   st: Code to generate dummy variable from several categorical variables?
Date   Sun, 15 Jan 2012 12:32:27 -0800 (PST)

Hello,

I'm looking for code to generate a dummy variable from values of several categorical variables which are not mutually exclusive. I've tried to do this using the generate and replace commands but Stata is changing the number of subjects in each dummy category as follows:

generate abnlX=.
(356 missing values generated)

replace abnlX=0 if A==0
(240 real changes made)

replace abnlX=1 if B==1
(80 real changes made)

tabulate abnlX

    abnlX   |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |        240       75.00       75.00
          1 |         80       25.00      100.00
------------+-----------------------------------
      Total |        320      100.00

replace abnlX=2 if C==1
(74 real changes made)

tabulate abnlcat

    abnlX   |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |        240       68.77       68.77
          1 |         35       10.03       78.80
          2 |         74       21.20      100.00
------------+-----------------------------------
      Total |        349      100.00

As you can see, the number of subjects for abnlX=1 changes from 80 to 35 as they are not mutually exclusive. This also occurs if I try to use parentheses:

replace abnlX=2 if C==1 & (B==. | B==0 | B==1)
(74 real changes made)

. tabulate abnlcat

    abnlX   |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |        240       68.77       68.77
          1 |         35       10.03       78.80
          2 |         74       21.20      100.00
------------+-----------------------------------
      Total |        349      100.00

Thank you,
Deborah

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index