Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: simple calculations across rows

From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: simple calculations across rows
Date   Mon, 16 Jul 2012 15:58:33 +0100

Better to say you are a beginning user, as you have not yet learned
how to spell "Stata"!!!

That detail aside, you can do things like this:

egen mean_1 = mean(measure / (day == 1 & inrange(period, 3, 4))), by(id)
egen mean_2 = mean(measure / (day == 2 & inrange(period, 3, 4))), by(id)

egen tag = tag(id)

scatter mean_2 mean_1 if tag

On the first device, note that an expression like

(day == 1 & inrange(period, 3, 4))

is 1 when true and 0 when false; correspondingly

measure / expression is measure when expression is 1 and missing (and
so ignored) when expression is 0. For more, see

Cox, N.J. 2011. Speaking Stata: Compared with ...Stata Journal 11(2): 305-314

Note that tagging is necessary to avoid each summary being used 12 times.


On Mon, Jul 16, 2012 at 3:37 PM, B. Timothy Walsh <[email protected]> wrote:

> I am a beginner-to-intermediate level STATA user, and would like to find a
> solution to what I hope is a relatively straightforward problem. I have
> looked a bit at the archives, and have been led in the direction of mata,
> but am wondering if another (less daunting?) method might be available.
> I am importing data from a device and would like to use STATA to do some
> data reduction. The device monitors types of physical activity over 2 days,
> and the data are provided in rows, each of which contains a summary of data
> over pre-defined 4 hour blocks, like 8 AM to 12N. So, each subject has 6
> rows of data/day and a total of 12 rows for 2 days.
> What I wish to do is to be able to summarize--like simple averages,
> etc.--across some but not all time periods, and, maybe, days. For example,
> for each subject, calculate the total activity over the 8AM to 4PM blocks on
> day 1 and compare that to the same measure on day 2. Etc.
> Here is a somewhat simplified outline of how one subject's data might look
> for the first 3 periods of day 1:
> ID      DAY             PERIOD  MEASURE
> i       1               1               Xi11
> i       1               2               Xi12
> i       1               3               Xi13
> I have only a modest dataset, so I do not need particularly efficient code.
> Better code that is easy to interpret and be modified by a novice such as
> myself.
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index