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]

From |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Generating a variable to indicate whether an array of variables is in increasing value |

Date |
Thu, 2 Jun 2011 09:33:20 +0100 |

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 <quangn@hawaii.edu> 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/

**Follow-Ups**:**Re: st: Generating a variable to indicate whether an array of variables is in increasing value***From:*Nick Cox <njcoxstata@gmail.com>

**References**:**st: Generating a variable to indicate whether an array of variables is in increasing value***From:*Mike <quangn@hawaii.edu>

- Prev by Date:
**st: Ado for determining structure of UK Postcodes** - Next by Date:
**Re: st: Generating a variable to indicate whether an array of variables is in increasing value** - Previous by thread:
**st: Generating a variable to indicate whether an array of variables is in increasing value** - Next by thread:
**Re: st: Generating a variable to indicate whether an array of variables is in increasing value** - Index(es):