Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: rescaling a variable by the maximum value question


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: rescaling a variable by the maximum value question
Date   Sat, 13 Oct 2012 12:45:38 +0100

"What Jay is missing" is better corrected to "What Jay understands" ...

On Sat, Oct 13, 2012 at 10:23 AM, Nick Cox <njcoxstata@gmail.com> wrote:
> Justina is correct. You can create a new variable, but there is no
> need to do that.
>
> su age, meanonly
> gen age2 = age / r(max)
>
> -su, meanonly- is more efficient when you just want a maximum. (The
> name -meanonly- is not indicative here.) That's unimportant in one-off
> cases, but a detail to note if doing this repeatedly.
>
> SJ-7-3  st0135  . . . . . . . . . . . Stata tip 50: Efficient use of summarize
>         . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
>         Q3/07   SJ 7(3):438--439                                 (no commands)
>         tip on using the meanonly option of summarize to more
>         quickly determine the mean and other statistics of a
>         large dataset
>
> What Jay is missing is that -max()- will in his context not return a
> scalar that is the maximum of a variable. In fact  Stata functions
> typically do not that, return a scalar that summarizes a variable.
> -max()- wants two or more arguments and will return the result of
> comparing them, as it were rowwise. If -max(x, y)- were calculated
> from two variables -x- and -y-, then there is scope for a new variable
> to be produced.
>
> Nick
>
> On Sat, Oct 13, 2012 at 9:02 AM, Justina Fischer <JAVFischer@gmx.de> wrote:
>> you can generate a variable that takes on the maximum value
>>
>> e.g.
>>
>>  su age
>> gen var = `r(max)'
>>
>> gen newvar = oldvar/var
>>
>>
>> see also return list after  the sum command
>
> "JVerkuilen (Gmail)" <jvverkuilen@gmail.com>
>
>>> I'm sure I'm simply overlooking something easy but I want to make a
>>> new variable that is rescaled by dividing by the maximum value of the
>>> old one. Old variable: 1, 2, 3, 4. New variable should be 0.25, 0.5,
>>> 0.75, 1.
>>>
>>> The syntax
>>>
>>>      gen newvar = oldvar/max(oldvar)
>>>
>>> does not work and I understand why it doesn't, because max doesn't
>>> work that way. Is there a straightforward way to do this? (Yes I could
>>> simply
>>>
>>>      summarize oldvar
>>>
>>> and pick out the max by hand and rescale. I'd like to avoid that, if
>>> possible. ;)
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index