Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: generate variable equal to first non-zero value in row
From
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: generate variable equal to first non-zero value in row
Date
Wed, 21 Aug 2013 16:59:58 +0100
(Revised version, thanks to comments by Eric Booth)
Nick
[email protected]
How would you do it by hand? You would look at each variable from left
to right until you find the first non-zero. So, Stata can be
instructed to do that too.
This code assumes no missings in -out*-. If missings are present, add
an extra condition -!missing(out`j')-.
gen first_nz = .
gen where_first_nz = .
qui forval j = 1/5 {
replace first_nz = out`j' if out`j' & first_nz == .
replace where_first_nz = `j' if out`j' & where_first_nz == .
}
Note here that
if `outj'
is short-hand for
if `outj' != 0
and we also need conditions such as
first_nz == .
If all values are zero, both variable remain set to missing.
>
> For a fairly detailed review of such questions, see
>
> SJ-9-1 pr0046 . . . . . . . . . . . . . . . . . . . Speaking Stata: Rowwise
> (help rowsort, rowranks if installed) . . . . . . . . . . . N. J. Cox
> Q1/09 SJ 9(1):137--157
> shows how to exploit functions, egen functions, and Mata
> for working rowwise; rowsort and rowranks are introduced
>
> which is accessible at http://www.stata-journal.com/sjpdf.html?articlenum=pr0046
> Nick
> [email protected]
>
>
> On 21 August 2013 16:25, D-Ta <[email protected]> wrote:
>> Dear Statausers,
>>
>> I have a dataset with the variables id and and out1-out5 where the "out"
>> variables represent sequential outcomes. I would like to generate a variable
>> (in the example below named first_nonzero) which indicates the first outcome
>> in the sequence row that is non zero. If there are all zeros throughout,
>> that indicator variable should be set/remain missing.
>>
>> The following table illustrateswhat I am trying to do. How do I generate the
>> first_nonzero variable? (I was trying to work with the -egenmore- commands,
>> but couldnt figure out).
>>
>> id out1 out2 out3 out4 out5 first_nonzero
>> 1 0 0 0 2 2 2
>> 2 0 2 2 0 0 2
>> 3 0 0 0 0 0 .
>> 4 4 4 4 0 0 4
>> 5 3 0 2 2 2 3
>> 6 0 4 4 4 0 4
>>
>>
>> Many thanks,
>> Darjusch
>> *
>> * For searches and help try:
>> * http://www.stata.com/help.cgi?search
>> * http://www.stata.com/support/faqs/resources/statalist-faq/
>> * http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/