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   "Sebastian F. B�chte" <[email protected]>
To   [email protected]
Subject   Re: st: First non-zero observation within a variable
Date   Sun, 4 Feb 2007 15:32:06 +0100

Thomas,

I hope I understood you problem correctly. Here is my solution to it:

// create variable with all entries set to missing
gen first=.

//check each entry within group (sorted by time) whether it is
//non-zero and non-missing and whether it is the first one which
//fulfills this condition, if so record the _n for it to variable first
//if not so record the entry in first from the preceding observation
//in it.
bys group (time): replace first=cond(expression>0 & expression<. &
first[_n-1]==.,_n,first[_n-1])

//drop all obs with first == missing
drop if first==.

hth
Sebastian


On 2/4/07, Thomas Speidel <[email protected]> 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
..

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/

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