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

st: a program to make dummy variables


From   "Lim, Nelson" <nlim@rand.org>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: a program to make dummy variables
Date   Wed, 8 Sep 2004 17:13:55 -0700

Hope you can help me. 

I am trying to create dummies variables from a categorical variable and
want to have value labels of the categorical variable to be the names of
the dummy variables.

For example, I have a variable called race_n:

      Numeric |
   version of |
         race |      Freq.     Percent        Cum.
--------------+-----------------------------------
        Asian |      1,692        3.19        3.19
        White |     41,311       77.90       81.09
     Hispanic |      2,237        4.22       85.30
        Black |      6,770       12.77       98.07
Native Indian |        272        0.51       98.58
        Other |        752        1.42      100.00
--------------+-----------------------------------
        Total |     53,034      100.00

I want to create 6 dummies whose names are the value labels of race_n.
For example, I would like to have the first dummy variable to be called
Asian. 

I wrote a program called my_dummy. It seems to work, but when I describe
the data, I get the following. The dummies only take the first letter of
the variable.

. describe

------------------------------------------------------------------------
----              storage  display     value
variable name   type   format      label      variable label
------------------------------------------------------------------------
----
A               byte   %8.0g                  race_n == 1
C               byte   %8.0g                  race_n == 2
H               byte   %8.0g                  race_n == 3
N               byte   %8.0g                  race_n == 4
T               byte   %8.0g                  race_n == 5
X               byte   %8.0g                  race_n == 6
------------------------------------------------------------------------
----
 

/* beginning of the program */
program define my_dummy

version 8

/* computing the maximum value of the variable */
tempvar max1
egen `max1'=rmax(`1')
tempvar max2
egen `max2'=max(`max1')
local maxval=`max2'

/* generating the set of dummy variables */
forvalues i = 1/`maxval' {
egen resp`i' = eqany(`1'), v(`i')
}


/* naming the value labels of the original variable */
 */ to the dummy variables

tokenize `1'
local j = 1
forvalues i = 1/`maxval' {
local labval`j' : label `1' `i'
local j = `j' + 1
}

local i 1
local j 1
while `i' == `j' & `i' <= `maxval' {
rename resp`i' `labval`j''
local i = `i' + 1
local j = `j' + 1
}


end

my_dummy race_n


. describe

------------------------------------------------------------------------
----              storage  display     value
variable name   type   format      label      variable label
------------------------------------------------------------------------
----
A               byte   %8.0g                  race_n == 1
C               byte   %8.0g                  race_n == 2
H               byte   %8.0g                  race_n == 3
N               byte   %8.0g                  race_n == 4
T               byte   %8.0g                  race_n == 5
X               byte   %8.0g                  race_n == 6
------------------------------------------------------------------------
----


This email message is for the sole use of the intended recipient(s) and
may contain confidential information. Any unauthorized review, use,
disclosure or distribution is prohibited. If you are not the intended
recipient, please contact the sender by reply email and destroy all copies
of the original message.


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