Statalist


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

st: RE: Data Management


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Data Management
Date   Wed, 15 Apr 2009 17:28:46 +0100

What you seek are often called "spells", in this case periods in which
prices remain constant. There are many other names too, including
"runs". 

My reason for using the first term is that Stata resources are available
under that heading:

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

tsspell from http://fmwww.bc.edu/RePEc/bocode/t
    'TSSPELL': module for identification of spells or runs in time
series /
    tsspell examines the data, which must be tsset time series, to /
identify
    spells or runs, which are contiguous sequences defined / by some
    condition. tsspell generates new variables indicating / distinct
spells,

With -tsset- data and -tsspell- from SSC, a solution is 

tsspell P_t 
gen change = _seq - 1 

Nick 
[email protected] 

Rijo John

 I have a time series data set as follows.

ID	year	P_t-1	P_t
1	1990	16	16
1	1991	16	16
1	1996	16	16
1	2005	42	42
1	2006	42	42
1	2007	42	42
2	1990	29	29
2	1991	29	29
2	1992	29	29

Where P_t and P_t-1 represents price in year t and t-1 respectively.
What I wish to achieve is to create a new variable "Change". This
variable should show the number of years passed since the last change
in P_t. In the above example the data should finally look like below.

ID	year	P_t-1	P_t Change
1	1990	 .	16      0
1	1991	16	16      1
1	1996	16	16      2
1      1997 16     16     3
1	2005	16	42      0
1	2006	42	42      1
1	2007	42	42      2
2	1990	 .	29      0
2	1991	29	29      1
2	1992	29	29      2
2      1993 29     35     0

If I do

bysort ID P_t : gen Change=_n-1 I get the varible numbered but it
doesnt sort the year. If I sort ID and year then I cant use the _n to
generate these numbers.

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index