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

# Re: st: From: John Singhammer <singhammer@gmail.com>

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: From: John Singhammer Date Thu, 16 Aug 2012 15:19:45 +0100

```It is recommended that you use informative titles for your posts.

If for each child there are only two observations, then the difference
between heights is

bysort id (grade) : gen diff = height[2] - height[1]

A safer, or at least more general, calculation is

egen height1 = mean(height / (grade == 1)), by(id)
egen height0 = mean(height / (grade == 0)), by(id)
gen diff2 = height1 - height0

That block of code could be compressed, with some danger to its understanding.

Values of -diff- will in each case be duplicated for all observations
with the same -id-, so we can see all observations for retrograde
children by

list if diff2 < 0

For a more general discussion of technique, including the division
trick used above, see

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

Abstract.  Many problems in data management center on relating values
to values in other observations, either within a dataset as a whole or
within groups such as panels. This column reviews some basic Stata
summarize, by:, sum(), cond(), and egen. Several techniques exploit
the fact that logical expressions yield 1 when true and 0 when false.
Dividing by zero to yield missings is revealed as a surprisingly
valuable device.

On Thu, Aug 16, 2012 at 3:01 PM,  <owner-statalist@hsphsun2.harvard.edu> wrote:

> I'm struggling with a problem that probably is easily solved.
> Apparently not by me, so any advices will be greatly appreciated
>
> The data looks like this: Children's height was measured at grade 0 and
> Usually, you expect childrens height to increase. however, for id a19,
> height decreased,  most likely due to measurement error
>
>
>
>  id maxheight0 maxheight1 height grade
> a19   130         .                 130       0
> a19     .         126               126       1
> y03    117         .                117       0
> y03     .         127               127       1
>
> I have 15.000 observation and wants to generate a variable of the
> difference in height from grade 0 to grade 1. That way, I can delete or
> correct those with a negative growth
> I've managed to isolate the measures for each grade but the next step is a
> mystery. Obviously, subtracting maxeighth1 from maxheight0 is not an obtion
> at this point. How do I manage to illuminate the difference in