Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: First non-zero observation within a variable


From   "Svend Juul" <[email protected]>
To   <[email protected]>
Subject   Re: st: First non-zero observation within a variable
Date   Sun, 4 Feb 2007 15:38:30 +0100

Thomas Speidel wrote:
 
I am trying to tag the first non-zero, non-missing observation within a
variable by group.  The goal is to drop anything BEFORE the expression
is measured within a group.  For example:
 
group   time    expression
1       1        0
1       2        0
1       3        3.2
1       4        4.1
1       5        0
1       6        2.5
2       1        0
2       2        .
2       3        0
2       4        3.7
2       5        4.0
..
 
My goal is to obtain this:
group   time    expression
1       3        3.2
1       4        4.1
1       5        0
1       6        2.5
2       4        3.7
2       5        4.0
..
-------------------------------------------------------
 
Doesn't this do what you want?
 
   sort group time
   by group: gen x=sum(expression)
   keep if x>0
 
This requires that -expression- can not be negative. If that could happen, you should create a help variable being 1 when -expression- is neither zero nor missing:
 
   gen z = expression!=0 & expression<.
   by group: gen x=sum(z)
   keep if x>0
 
Hope this helps
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