Statalist The Stata Listserver


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

st: First non-zero observation within a variable


From   Thomas Speidel <[email protected]>
To   [email protected]
Subject   st: First non-zero observation within a variable
Date   Sat, 03 Feb 2007 22:38:35 -0700

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
..

I would like to avoid reshaping the dataset (where the useful "egen
rfirst" would help).  I have tried using various subscripting
expressions, but none produced the end result.

bysort group: gen firstexp=1 if expression[_n] > expression[n-1]
bysort group: gen cum=sum(firstexp)
gen tag=firstexp-cum
gen first=tag==0

The above commands gets me one step closer, but it still doesn't get me
there...

Thanks,

Thomas Speidel

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