Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Xi or Desmat


From   wgould@stata.com (William Gould, Stata)
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Xi or Desmat
Date   Wed, 23 Jul 2003 08:30:28 -0500

Joao Pedro <jazevedo@provide.com.br> asks, 

> I would like to know if there is any command which would automatically
> generate dummy variables out of a categorical variable. [...]

Yes there is.  The command is -tabulate- with the -generate(<varnamestub>)-
option.  For instance, typing 

        . tabulate rep78, gen(rr)

would present a one-way tabulation of categorical variable rep78 *AND* it
would create new 0/1 variables named rr1, rr2, ...  Variable rr1 will contain
1 when rep78 contains its first value and 0 when rep78 contains another,
nonmissing value; rr2 will contain 1 when rep78 contains its second value and
0 when rep78 contains another, nonmissing value; and so on.  The new variables
will contain . where rep78 contains missing.

Here's an example:

        . sysuse auto
        (1978 Automobile Data)

        . tabulate rep78, gen(rr)

             Repair |
        Record 1978 |      Freq.     Percent        Cum.
        ------------+-----------------------------------
                  1 |          2        2.90        2.90
                  2 |          8       11.59       14.49
                  3 |         30       43.48       57.97
                  4 |         18       26.09       84.06
                  5 |         11       15.94      100.00
        ------------+-----------------------------------
              Total |         69      100.00

        . describe rr*

                      storage  display     value
        variable name   type   format      label      variable label
        ----------------------------------------------------------------
        rr1             byte   %8.0g                  rep78==     1.0000
        rr2             byte   %8.0g                  rep78==     2.0000
        rr3             byte   %8.0g                  rep78==     3.0000
        rr4             byte   %8.0g                  rep78==     4.0000
        rr5             byte   %8.0g                  rep78==     5.0000

        . summarize rr*

            Variable |       Obs        Mean    Std. Dev.       Min        Max
        -------------+--------------------------------------------------------
                 rr1 |        69    .0289855    .1689948          0          1
                 rr2 |        69     .115942    .3225009          0          1
                 rr3 |        69    .4347826    .4993602          0          1
                 rr4 |        69    .2608696    .4423259          0          1
                 rr5 |        69    .1594203    .3687494          0          1

-- Bill
wgould@stata.com
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index