Statalist


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

st: RE: problem with looping egen(newvar)=anymatch


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: problem with looping egen(newvar)=anymatch
Date   Wed, 20 Jan 2010 21:59:12 -0000

The error is easy to explain. Imagine you are Stata. Second time round
the loop -fathliv- already exists, so you can't create it afresh. 

Otherwise I see no need for and no point in a loop. Just specify the
varlist. Very likely 

egen fathliv = anymatch(fammem*), v(2) 

catches what you want. 

For the record, an equivalent loop is 

gen fathliv = 0 

qui foreach v of var fammem* { 
	replace fathliv = 1 if `v' == 2 
}

That's actually much less Stata than implied by the -egen- command,
although more to type. 

See also 

SJ-9-1  pr0046  . . . . . . . . . . . . . . . . . . .  Speaking Stata:
Rowwise
        (help rowsort, rowranks if installed) . . . . . . . . . . .  N.
J. Cox
        Q1/09   SJ 9(1):137--157
        shows how to exploit functions, egen functions, and Mata
        for working rowwise; rowsort and rowranks are introduced

Nick 
[email protected] 

Ekaterina Hertog

I need to create a binary variable that equals 1 of one of the variables

fammem1, fammem2, fammem3, fammem4, ... fammem10 equals 2, and 0 
otherwise.#I came up with the following solution:
egen fathliv = anymatch(fammem1 fammem2 ... fammem10), v(2)
and it seems to work, but I would like to create a loop rather than 
listing the ten fammem variables individually.
I tried:
forv i=1/10{
egen fathliv = anymatch(fammem`i'), v(2)
}
But this gives me an error message and I am not sure why.

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