Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: compare variables


From   "Austin Nichols" <austinnichols@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: compare variables
Date   Thu, 30 Aug 2007 11:05:17 -0400

interlocutor apparently named Stat List:

Actually, your initial request is one character from a solution:
g var3=max(var1,var2)
and you should read the online help for -generate- (just type -h g-,
as h is an abbreviation for help and g is an abbreviation for
generate) and related commands.  -egen- provides the rowmax()
rowtotal() and other functions, but has some overhead that slows it
down. Both the max() function and the rowmax() egen functions treat
missing as negative infinity, while the running-sum function sum() and
the -egen- functions total() and rowtotal() will treat missing as zero
(as is usually desired).  This can create problems for some
calculations, however, so you would be wise to examine cases where one
or both variables are missing, unless you are confident the default
behavior is what you need for your application:

li var1 var2 var3 if mi(var1,var2)

(see -h mi- and -h missing- for more).

ps. You should probably change your email alias to a real or
real-sounding name or pseudonym for posting to the list.

On 8/30/07, Neil Shephard <nshephard@gmail.com> wrote:
> > 2007/8/30, Stat List <statlist@gmail.com>:
> > > Hi Statalist,
> > >
> > > If I have two variables with numeric contents, is there any Stata command to
> > > compare them, and get the maximum into another variables?
> > >
> > > Example, comparing (elements by elements) var1 and var2, and then creating
> > > var3=max(var1, var2)

> You can do this directly using the -egen- command.
>
> egen var3 = rowmax(var1 var2)
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index