# st: RE: Re: replacing values in a variable with rowsums, aka "ereplace"

 From "Nick Cox" To Subject st: RE: Re: replacing values in a variable with rowsums, aka "ereplace" Date Tue, 15 Jul 2008 12:08:54 +0100

```Note that this won't work as code like

if y = `n'

needs to be

if y == `n'

and

forvalue n = 1/n {

won't work either. Kim did say "something like".

Beyond that, I don't see what point Kim is making here. This code can be
collapsed to

egen x = rowtotal(d1-d40)

Why create separate variables and put them together when you can get
there in one?

Nick
n.j.cox@durham.ac.uk

Kim Lyngby Mikkelsen (KLM)

If you have consecutive values, you should try to use -forvalue-,
otherwise you could try to use -foreach-.

If you have y=1, y=2, ..., y=n, you could try to do something like this

forvalue n = 1/n {
egen x_`n'=rowtotal (d1-d40) if y=`n'
}
gen x=.
forvalue n = 1/n {
replace x=x_`n' if y=`n'
drop x_`n'
}

Ora John Reuter

I am searching for something that would do the equivalent of an
'ereplace'.  I have a set variables for which I generated a -rowtotal-
given some condition, e.g. (egen x=rowtotal (d1-d40) if y=1), but then
I need to replace some values in that same variable (x) with a
different -rowtotal- given a different condition.  I dont know how else
to explain it except to say that if there were an  'ereplace' command
the next command would look like this:

"ereplace" x=rowtotal(d2-d40) if y=2

In other words, I need to replace values in x with different sums of
variables.  I need to do this for a great many conditions so typing out
the list of variables to be added for each condition would be a minor
pain.

*
*   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/
```