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]

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

To |
statalist@hsphsun2.harvard.edu |

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/

**Follow-Ups**:**Re: st:any easy alternative way when -egen- is not allowed to combine***From:*Nick Cox <njcoxstata@gmail.com>

**Re: st:any easy alternative way when -egen- is not allowed to combine***From:*Nick Cox <njcoxstata@gmail.com>

- Prev by Date:
**st: -romantoarabic- now available from SSC** - Next by Date:
**Re: st: Assigned sex** - Previous by thread:
**st: -romantoarabic- now available from SSC** - Next by thread:
**Re: st:any easy alternative way when -egen- is not allowed to combine** - Index(es):