Bookmark and Share

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   "Eric A. Booth" <[email protected]>
To   [email protected]
Subject   Re: st: generate variable equal to first non-zero value in row
Date   Wed, 21 Aug 2013 10:51:16 -0500

<>

Nick's advice is slightly different than mine.  His gets you the "out"
variable with the value of interest as the value in first_nonzero --
mine gets you value the first "out" variable holds.
Based on the column of values the OP puts in the example dataset, I
_think_ he wants an approach closer that what I suggested, but you can
still use Nick's code and add this to get the values of interest
(though I feel like there's a faster way to get there than my code
below):

forval n = 1/`=_N' {
   if !mi(`=first_nonzero[`n']') {
      replace first_nonzero = out`=first_nonzero[`n']' in `n'
       }
}

- Eric

On Wed, Aug 21, 2013 at 10:35 AM, Nick Cox <[email protected]> wrote:
> 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.
>
> gen first_nonzero = .
>
> qui forval j = 1/5 {
>        replace first_nonzero = `j' if out`j'  & first_nonzero == .
> }
>
> Note here that
>
> if `outj'
>
> is short-hand for
>
> if `outj' != 0
>
> and we also need the condition that
>
> first_nonzero == .
>
> If all values are zero, the variable remains 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/
*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index