Statalist


[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   Martin Weiss <[email protected]>
To   <[email protected]>
Subject   Re: st: how to add 23 in each row of a variable
Date   Mon, 15 Sep 2008 20:42:52 +0200

Well, I have not said that it is evil, but it is certainly not the first tool I think about when confronted with a new data management problem in Stata. The -in- qualifier is usually the solution for me...

Martin Weiss
_______________________
----- Original Message ----- From: "Eva Poen" <[email protected]>
To: <[email protected]>
Sent: Monday, September 15, 2008 8:32 PM
Subject: Re: st: how to add 23 in each row of a variable



I disagree, at least in part. Subscripting is extremely powerful, and
I use it on a regular basis with grouped data or panel data. Fabian's
attempt to do a simple -replace- in a loop reminds me of what some of
my colleagues do in their spreadsheet applications: a cell equals the
cell to the left of it + 23, then copy it down til the end of the
column.

I'm not sure what NC101 actually covers, but I'd imagine it helps
users that come from a spreadsheet application by showing how much
simpler (and safer) things work in Stata, and also providing them with
the associated lingo (variables, observations etc, as Nick has pointed
out repeatedly in the last weeks).

Subscripting is not necessary in most simple data management tasks,
but it is not evil, either.

Eva



2008/9/15 Martin Weiss <[email protected]>:
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 a
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 Stata...

Martin Weiss
_______________________
----- Original Message ----- From: "Eva Poen" <[email protected]>
To: <[email protected]>
Sent: Monday, September 15, 2008 8:10 PM
Subject: Re: st: how to add 23 in each row of a variable



Fabian,

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-.

Eva


2008/9/15 Fabian Brenner <[email protected]>:

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:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
*
*   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