Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Re: Replace current values matching certain condition using values from other observations?
From
"Joseph Coveney" <[email protected]>
To
<[email protected]>
Subject
Re: st: Re: Replace current values matching certain condition using values from other observations?
Date
Sun, 26 Jan 2014 03:03:43 +0900
Chris Yang wrote:
As a related question, if I have many variables that need to be
replaced like this, is there a quick way to do them all at once
instead of doing it one-by-one for each variable. Perhaps I could use
a loop to loop through the variables, but if all the variable names
follow a pattern, e.g. var*, is there a shortcut?
I've tried: bysort group (seq): replace var*=var*[1] if seq > 1 for
the example given, but it gives an error saying: "var ambiguous
abbreviation".
--------------------------------------------------------------------------------
You'd use a -foreach- or -forvalues- loop over the variables. Either
sort group seq
foreach var of varlist var* {
by group: replace `var' = `var'[1] if seq > 1
}
or, if the variable names are distinguished by a numerical suffix, then
sort group seq
forvalues i = 1/3 {
by group: replace var`i' = var`i'[1] if seq > 1
}
Joseph Coveney
*
* 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/