Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: Creating a new variable by combining several


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: Creating a new variable by combining several
Date   Wed, 6 Feb 2008 10:23:34 -0000

Suppose the definition of a set of dummy variables was more complicated
than in this well-behaved example. 

Say -x- takes on values 11, 13, 17, 19, 23 and dummies -x1- to -x5-
correspond to those values, i.e. -x1- is 1 iff -x- is 11, and so forth.
But of course -x- is not in your dataset; if it were you would not be
doing this. 

The look-up technique could be something like this. Use -tokenize- on
the list of values to assign them to local macros 1 to 5. 

tokenize "11 13 17 19 23" 
gen x = . 
qui forval i = 1/5 { 
	replace x = ``i'' if x`i' == 1 
} 

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Nick Cox
Sent: 05 February 2008 22:16
To: [email protected]
Subject: st: RE: Creating a new variable by combining several

Good. The generalisation to other sets of dummies should be clear. If
there are twenty dummies, a loop may be more congenial: 

gen byte whichpet = pet1 
qui forval i = 2/20 { 
	replace whichpet = whichpet + `i' * pet`i' 
}


*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index