Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: generate a new categorical variable


From   Ronnie Babigumira <rb.glists@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: generate a new categorical variable
Date   Thu, 13 Sep 2012 22:16:15 +0200

Your code works just fine using this small dummy dataset 

clear
input Y str1 Z
1 A
1 B
0 C
1 D
1 Y
end
gen X=0 if Y==0
replace X=1 if Y==1 & Z=="A"
replace X=2 if Y==1 & Z=="B"
replace X=3 if Y==1 & Z=="C"
replace X=4 if Y==1 & Z=="D"

list
      Y Z X 
1. 1 A 1 
2. 1 B 2 
3. 0 C 0 
4. 1 D 4 
5. 1 Y .

PS: You may find -help cond- or even better http://www.stata-journal.com/sjpdf.html?articlenum=pr0016 useful for this type of problem 

-- 
010100100110111101101110011011100110100101100101




On Thursday, September 13, 2012 at 9:53 PM, urbain thierry YOGO wrote:

> Dear all
> i have two variables Y and Z. Y is a dummy variable which takes 1 if
> individual has access to water and 0 otherwise. Z is a categorical
> variable which has the following structure:
> Z=A if individual uses bleach to clean water
> Z=B if individual use a filter to clean water
> Z=C if individual use solar energy to clean water
> Z=D if individual use absorbent cotton to clean water
> 
> Here is my problem. I want to generate a new variable X which should
> be a combination of Y and Z. Here is the code i have written
> gen X=0 if Y==0
> replace X=1 if Y==1 & Z=="A"
> replace X=2 if Y==1 & Z=="B"
> replace X=3 if Y==1 & Z=="C"
> replace X=4 if Y==1 & Z=="D"
> 
> My concern is that when i write this code, the first two lines work,
> but after, there is 0 replacement. Please can somebody help me? Thank
> 
> -- 
> *Urbain Thierry YOGO
> Ph.D candidate in Economics*
> *
> * 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/



*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index