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:20:09 +0000

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()- --

-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

or

SJ-1-1  pr0003  . . . Speaking Stata: How to repeat yourself without going mad
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q4/01   SJ 1(1):86--97                                   (no commands)
        describes systematic ways of repeating the same or similar
        operations

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