Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <njcoxstata@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: Creating Dummys from a variable with 5 categories |
Date | Sun, 2 Dec 2012 16:49:26 +0000 |
As Daniel Klein's posting also indicated, this procedure is a little risky and may give incorrect results if you have missing values. Usually you want missing values to be reflected by missing values in the indicator. A more general definition, which allows a one-line Stata solution, is gen <dummy> = cond(missing(<varname>), ., <expression>) when you want the <dummy> to be 1 if <expression> is true and 0 if it is false. For example gen flatfee = cond(missing(myvar), ., myvar == 1) gen nonflatfee = cond(missing(myvar), ., myvar == 0) As said, you may know that you want missing values to imply 1 or 0 for your indicator, in which case you can supply the appropriate answer in place of . (system missing). Here and elsewhere, I recommend the term "indicator" over "dummy". I have heard too many stories in which "dummy" was misinterpreted as implying disparagement, insult or offence, but no such stories about "indicator". Nick On Sun, Dec 2, 2012 at 3:53 PM, JVerkuilen (Gmail) <jvverkuilen@gmail.com> wrote: > Your question isn't quite specified to be answerable but here's a > quick way to make a dummy variable that has value 1 for flat fee and 0 > for any other value of myvar: > > generate flatfee = 1 > replace flatfee = 0 if myvar != 1 > > What this syntax does is (a) make a variable flatfee that has 1 > everywhere and (b) change all values for which myvar does not indicate > a flat fee to 0. You could also > > generate flatfee = 0 > replace flatfee = 1 if myvar = 1 > > which is the opposite process, as it changes the 0s to 1s where flat > fee is applicable. This seems more natural than coding flat fee the 0 > and all other methods 1, but if you want the other way around > > generate nonflatfee = 1 - flatfee > > Try these and then check your work by > > tabulate flatfee myvar > > which is always a good idea whenever you're recoding variables. You > should see a 2x5 table that has the counts all in the right place. > > > On Sun, Dec 2, 2012 at 3:27 AM, Sharon Brody <brodysharon@yahoo.co.uk> wrote: >> I have a variable with 5 answers and need to make a dummy variable from it >> >> The question asked - How are you charged for your waste: >> 1. Flat Fee >> 2. Weight or Volume >> 3. Frequency >> 4. Not Charged >> 5. Dont Know >> >> If I want to create a dummy variable that uses Flat Fee as a reference point >> >> 1. How do I create a dummy (I have tried the basic way - gen XXXX =1 replace XXXX=0 if YYYY=>2) but I believe this is incorrect as it doesnt incorporate all the variables. >> >> 2. How do I get Stata 12 to use "Flat Fee" as the reference base point >> * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/