# st: RE: RE: Placing a non-missing value at beginning of a block

 From "Nick Cox" To Subject st: RE: RE: Placing a non-missing value at beginning of a block Date Wed, 10 Dec 2008 18:01:09 -0000

```The second condition is equivalently in this problem

!missing(pvalue[_n-1])

Nick
n.j.cox@durham.ac.uk

Nick Cox

Each block of observations starts either (a) at the first observation or
(b) with a missing value not equal to the previous value.

A block identifier 1, 2, etc. is thus obtained by

gen block = sum((_n == 1) | ((pvalue != pvalue[_n-1]) &
missing(pvalue)))

Then

sort block pvalue n

and the observations are in the order you desired.

Whether you need to use -n- as well I am not clear.

Note that the first observation is special as pvalue[0] is returned as
missing and so is equal to pvalue[1].

In some ways it would be easier to work with the reversed data set. Then
each block starts with a non-missing value. But I don't think the total
code would be easier.

Related tricks were discussed in excruciating detail within

SJ-7-2  dm0029  . . . . . . . . . . . . . . Speaking Stata: Identifying
spells
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N.
J. Cox
Q2/07   SJ 7(2):249--265                                 (no
commands)
shows how to handle spells with complete control over
spell specification

Nick
n.j.cox@durham.ac.uk

Thomas Speidel

I am trying to create a spreadsheet ready table from within Stata and
need to do some data management in order to go from this:

n	pvalue
1	.
2	.
3	.
4	.
5	0.151
6	.
7	.
8	.
9	.
10	0.000
11	.
12	.
13	.
14	.
15	0.146
16	.
17	.
18	.
19	.
20	.
21	0.001
...

To this:

n	pvalue
1	0.151
2	.
3	.
4	.
5	.
6	0.000
7	.
8	.
9	.
10	.
11	0.146
12	.
13	.
14	.
15	.
16	0.001
17	.
18	.
19	.
20	.
21	.
...

Notice that the blocks are not constant (some are 5, some are 6 etc.
-this is just a portion of the data-).  My difficulty lies in the fact
that there is no natural grouping variable.  I tried a mixture of
subscripting and sequence generating commands, but did not get too far.
The crucial part is that I need to place the non-missing pvalue at the
beginning of the block, while right now it is at the end.

*
*   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/
```