In one of many terminologies, each switching point defines the start of
a new spell. Thus in addition,
1. -tsspell- from SSC is a useful tool. The variable desired by Quang
Nguyen is just _seq - 1, in terms of variables created automatically by
-tsspell-.
2. Independently of that, a fairly detailed discussion of techniques for
handling spells was given in
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
By the way, and this is a crucial detail:
I am guessing what Martin's code and Quang's example don't respect: that
you want to do this separately by identifier.
Nick
[email protected]
Martin Weiss
Also look at the techniques provided by Nick in the FAQ:
http://www.stata.com/support/faqs/data/#manip
Martin Weiss
*************
clear*
input byte(id x)
1 1
1 1
1 1
1 1
1 0
1 0
1 1
1 1
2 1
end
compress
gen byte y=0
replace y= /*
*/ cond(x[_n-1]!=x[_n], 0, /*
*/ y[_n-1]+1) in 2/l
list, noobs
*************
Quang Nguyen
Suppose, I have the following data:
id x
1 1
1 1
1 1
1 1
1 0
1 0
1 1
1 1
2 1
..
Now, I would like to generate a variable _y _such that _y_ takes the
ordering value of 1, 2, 3... as long as the corresponding _x_ takes
the same value as the previous observation. Otherwise, _y_ is 0.
Specifically, from the above data, we have:
id x y
1 1
1 1 1
1 1 2
1 1 3
1 0 0
1 0 1
1 1 0
1 1 1
2 1 2
..
Can you suggest a way to do this in 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/