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: computation across rows

 From Nick Cox <[email protected]> To [email protected] Subject Re: st: computation across rows Date Fri, 11 Feb 2011 01:10:05 +0000

```I find it hard to see a general pattern under your question. Your toy
example would seem easiest to solve by mental arithmetic in the Data
Editor, but you wouldn't be asking if that were true of your real
problem.

Naturally you can just find the subscripts for each observation and
use those, but again I assume from your question you know that you can
do that.

Some problems a bit like this benefit from a variable containing the
observation number:

gen long obs = _n

Then you can find the observation number for -id- 85, etc.

su obs if id == 82, meanonly
local obs82 = r(min)
su obs if id == 85, meanonly
local obs85 = r(min)
su obs if id == 89, meanonly
local obs89 = r(min)

The assumption here is that each identifier occurs once only so you
can indifferently pick up r(min), r(max) or r(mean) after -summarize-.
Then you can do things like

replace Y = Y[`obs85'] + Y[`obs89'] in `obs82'

See also

SJ-6-4  dm0025  . . . . . . . . . .  Stata tip 36: Which observations? Erratum
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
Q4/06   SJ 6(4):596                              (no commands)
correction of example code for Stata tip 36

SJ-6-3  dm0025  . . . . . . . . . . . . . .  Stata tip 36: Which observations?
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
Q3/06   SJ 6(3):430--432                                 (no commands)
tip for identifying which observations satisfy some
specified condition

Nick

On Thu, Feb 10, 2011 at 8:17 PM, Hewan Belay <[email protected]> wrote:
> Dear Statalist,
>
> I am trying to do something I expected to be very simple, but I'm not finding a
> straightforward way to do this. Essentially, I would like to do discrete
> computations across rows/observations (ie within variables). Here is an example
> of what I mean, consider this toy dataset (I hope the table is easily visible):
>
> id       Y    Z    W
> 81       4    1    3
> 82       .     0    9
> 85      2     4    1
> 87      3     1     4
> 89      6     2    5
>
> For the id #82, I want the variables Y and W to take on the value that results
> when adding their respective values for IDs #85 and 89. In the above toy
> example, that means that the missing value would become an 8, and the value of 9
> would change to 6. I definitely don't want to xpose or reshape my data, as I
> have several other operations I am doing on the data given its current
> structure.
>
>
> So generally speaking, my question is how to do computations across selected
> rows. I only have info on this with regard to computations X rows above or
> beyond the concerned row, that is using the operation [n+1], or when getting
> statistics for groups of rows using the -by- command.
>

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index