[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   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: how to add 23 in each row of a variable
Date   Mon, 15 Sep 2008 19:32:10 +0100

I think there are two quite distinct points being conflated here. 

You can do an awful lot of Stata work without needing subscripts.
Perhaps many users never need them, and good luck. 

But it would be quite wrong to wonder whether they are ever necessary. 

Two quick examples: 

1. Any problem that hinges on looking at previous observations and uses
That makes some awkward problems trivial. Examples in

2. There are problems in which you do need to loop over observations. 
See SJ 7(4): 571-581 (2007) for one. 

[email protected] 

P.S. a correction to Eva's correction: 

replace number[`i'] = 

would have to be 

replace number = 

Martin Weiss

I see braod agreement on the solution. This thread is a tough reminder, 
though, of the difficulties that can sometimes seem insurmountable for 
beginners in Stata. Having had a distraught student walk into my office
couple of days ago with the exact same problem, I think it is extremely 
important to emphasize that Stata does not exactly "push" subscripting
as a 
solution to data management problems. I would have trouble recalling a 
single instance where subscripting has proved necessary for me in

Eva Poen
> 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
> 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 <[email protected]>:
>> how can I tell Stata to add 23 in each row of a variable in the
>> I do not know what I am doing wrong because it does not work like
>> local i=1
>> while `i'<_N {
>>  2. replace number[i]= number[`i'] + 23
>>  3. }

*   For searches and help try:

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