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: Generating a variable to indicate whether an array of variables is in increasing value
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: Generating a variable to indicate whether an array of variables is in increasing value
Date
Thu, 2 Jun 2011 09:37:01 +0100
Alternatively, if the title of your posting is an exact indication of
your desire then
if !missing(x1, x2, x3, x4)
may be the only bit of code here that will help.
Nick
On Thu, Jun 2, 2011 at 9:33 AM, Nick Cox <[email protected]> wrote:
> You don't say what you want if e.g. x1 > x3 > x2 > x4.
>
> Nor do you spell out what you want to happen if there are equalities.
>
> Nor do you spell out exactly what you want to happen if there are missings.
>
> The last is easiest: I guess you want missings to map to missings.
>
> On the first I guess that you want 1, 2, 3, 4 as results if x1, x2,
> x3, x4 is largest.
>
> Any way, here is some technique.
>
> gen which = 1 if !missing(x1, x2, x3, x4)
> gen largest = x1
>
> forval j = 2/4 {
> replace which = `j' if x`j' < . & x`j' > largest
> replace largest = max(x`j', largest)
> }
>
> Note that if x1 == x2 and that both are larger than x3, x4 then
> -which- ends up with 1 here. For the opposite rule that 2 trumps 1 if
> both tie use
>
> x`j' >= largest
>
> above. Note also that -max(,)- does the right thing for you and
> ignores missings as far as possible so that -max(42, .)- is 42 but
> -max(.,.)- is missing.
>
> For a much more detailed discussion see
>
> J-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
>
> and its -rowsort-, -rowranks- which may be downloaded regardless of
> whether you have access to the Stata Journal.
>
> Nick
>
> On Thu, Jun 2, 2011 at 4:52 AM, Mike <[email protected]> wrote:
>
>> We have a data set, that have 4 variables: x1, x2, x3, x4. Can you
>> help generating a variable y whose value is constructed as follows:
>>
>> y = 1 if x1>x2>x3>x4
>> y = 2 if x2>x1>x3>x4
>>
>> and so on.
>>
>> Sure, we can use something such as:
>>
>> gen y=.
>> rep y =1 if x1>x2 & x2>x3 & x3>x4
>>
>> However, there is missing value in some of the x variables. Can you
>> suggest some efficient solution for this question?
>>
>
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/