Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: identifying lengths of runs in a binary variable


From   "Svend Juul" <[email protected]>
To   <[email protected]>
Subject   Re: st: identifying lengths of runs in a binary variable
Date   Mon, 18 Apr 2005 14:22:40 +0200

Faye,
 
This sequence of commands does the trick. The obnsno
variable enables to return to the original sort order;
-gsort- sorted in reverse order so that each run
starts with the highest value of newvar, i.e. the
number of observations in that run.
 
clear
input binvar
1
1
1
0
0
1
1
1
1
0
end
gen obsno=_n
gen newvar=1
replace newvar=newvar[_n-1]+1 if binvar==binvar[_n-1]
gsort -obsno
replace newvar=newvar[_n-1] if binvar==binvar[_n-1]
replace newvar=. if binvar==0
sort obsno
list
 
No doubt this could be done with fewer commands.
 
Svend
________________________________________________________ 
 
Svend Juul
Institut for Folkesundhed, Afdeling for Epidemiologi
(Institute of Public Health, Department of Epidemiology)
Vennelyst Boulevard 6 
DK-8000 Aarhus C,  Denmark 
Phone, work:  +45 8942 6090 
Phone, home:  +45 8693 7796 
Fax:          +45 8613 1580 
E-mail:       [email protected] 
_________________________________________________________ 

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



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index