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

Re: st: how to add 23 in each row of a variable

From   "Eva Poen" <>
Subject   Re: st: how to add 23 in each row of a variable
Date   Mon, 15 Sep 2008 19:10:36 +0100


er, no need for a loop. May

replace number = number + 23

be what you want?

The code for your (unnecessary!) loop as a few mistakes which I am
going to correct anyway.
If you use -while-, you have to increment your counter macro inside the loop:
local i 1
while `i' < 100 {
 local ++i

Else you have an infinite loop, because the initial condition is
always true. -foreach- and -forvalues- are usually much better
choices, and run faster, I believe.

Your condition -while `i' < _N- leaves out the last observation. Next,
number[i] should read number[`i']. _However_, none of this is
necessary! Use -generate- and -replace-.


2008/9/15 Fabian Brenner <>:
> Hello,
> how can I tell Stata to add 23 in each row of a variable in the dataset?
> I do not know what I am doing wrong because it does not work like this:
> local i=1
> while `i'<_N {
>  2. replace number[i]= number[`i'] + 23
>  3. }
> Thanks for your help.
> Fabian
> P.S. Thanks a lot to Neil for the help.
*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index