# Re: st: RE: Converting span data to spell data

 From John-Paul Ferguson To statalist@hsphsun2.harvard.edu Subject Re: st: RE: Converting span data to spell data Date Wed, 28 Nov 2007 07:32:38 -0500

```Right. Of course! Expand! Thank you, Nick--the cold is indeed blocking the
obvious.

The real data has elapsed dates for the start and end values, but it's easy to
modify this code with the appropriate date functions.

Best,
John-Paul

Quoting Nick Cox <n.j.cox@durham.ac.uk>:

```
```With your well-behaved example data, this is sufficient.

gen duration = end - start + 1
expand duration
bysort id (start) : gen period = _n

N.B. sorry about your cold, but it's summer for an important fraction of
the list!

John-Paul Ferguson

I am struggling with converting panel data from one form to another. The
current data structure does not seem to lend itself to a simple reshape.
I am also struggling with the first wave of the winter colds and flus,
which may be why the solution is eluding me. I'd greatly appreciate
ideas anyone has on the following problem.

Presume I have data in the following form:

id	start	end	true
a	1	4	1
a	5	6	0
a	7	10	1
a	11	13	0
a	14	16	1
b	1	3	1
b	4	7	0
b	8	10	1

I would like to put it in this form:

id	period	true
a	1	1
a	2	1
a	3	1
a	4	1
a	5	0
a	6	0
a	7	1
a	8	1
a	9	1
a	10	1
a	11	0
a	12	0
a	13	0
a	14	1
a	15	1
a	16	1
b	1	1
b	2	1
b	3	1
b	4	0
b	5	0

...and so on. This isn't quite the same as running reshape on the data,
because the data are not, technically, in a wide format.

I suspect I am not the first person to encounter this issue: I have data
that reports spans of time when some condition holds, and I want to
convert the data into spells. Is there a canned command, or a canny
combination of reshape and some other commands, that will do this?

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

```
```
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```