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:any easy alternative way when -egen- is not allowed to combine


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st:any easy alternative way when -egen- is not allowed to combine
Date   Wed, 22 Dec 2010 13:29:56 +0000

Apologies that my references were previously mangled. Here they are corrected.

On Wed, Dec 22, 2010 at 1:20 PM, Nick Cox <[email protected]> wrote:
> Here are some other ways to do it:
>
> bysort ID (indicator) : gen any_pos = indicator[_N]
>
> bysort ID : egen any_pos = max(indicator > 0)
>
> The key to the latter is realising that -max()- can take an
> expression. The expression in this example, indicator > 0, will
> evaluate to 1 or 0 as desired.
>
> For more discussion, see -- on the relationship between "any" and
> -max()- and "all" and -min()- --

FAQ     . . Creating variables recording whether any or all possess some char.
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        2/03    How do I create a variable recording whether any
                members of a group (or all members of a group)
                possess some characteristic?
                http://www.stata.com/support/faqs/data/anyall.html


or

SJ-2-1  pr0004  . . . . . . . . . . Speaking Stata:  How to move step by: step
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q1/02   SJ 2(1):86--102                                  (no commands)
        explains the use of the by varlist : construct to tackle
        a variety of problems with group structure, ranging from
        simple calculations for each of several groups to more
        advanced manipulations that use the built-in _n and _N


> The -egen- method is more easily adaptable to the case in which the
> variable in question might be missing.
>
> bysort ID : egen any_pos = max(indicator > 0 & indicator < .)
>
> Amanda Fu
> ========
>
> I notice  some options of -egen- are not allowed to combine with by.
> I just wondered if there is any good way to handle these situations.
> Let me use an example.
> ----------------------------------------
> ID        surveyYear       indicator (maximum value is 10)
> 1         1985                     0
> 1         1986                     1
> 1         1987                     2
> 2         1985                     0
> 2         1986                     0
> 2         1987                     0
> ...............
> ----------------------------------------
> I want to create a variable "new" that takes value 1 if there is at
> least one year for a ID's indicator is above 0  and takes the value 0
> (like ID 1) is all the years the indicator is 0 (like ID 2).
> What I wish to use is as following:
> . bysort id: egen  new=(indicator), anymatch(1/10)
> Is there any simple way to do this? Any comments will be helpful. Thank you!
>

*
*   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/


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