Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st:any easy alternative way when -egen- is not allowed to combine with by

 From Stas Kolenikov To statalist@hsphsun2.harvard.edu Subject Re: st:any easy alternative way when -egen- is not allowed to combine with by Date Tue, 21 Dec 2010 17:01:49 -0600

I think your second email gives the healthiest approach. Very little
of what -egen, by()- does cannot be done with a few lines of -bysort-,
and it is often more lucid than the -egen- code.

tempvar sumnew
bysort id: g `sumnew' =sum(indicator)
bysort id: g byte new=(sumnew>0,1,0)
assert indicator == 0 if new == 0
assert new == 1 if indicator > 0 & !missing(indicator)

On Tue, Dec 21, 2010 at 4:50 PM, Amanda Fu <mandy.fu1@gmail.com> wrote:
> Just a supplement to my question:
>
> I know I could create an intermediate variable as following:
>
> bysort id: g  sumnew=sum(indicator)
> bysort id: g  new=(sumnew>0,1,0)
>
> But I do not like this way because of the intermediate variable
> "sumnew". It is created for the purpose of getting "new". If I keep
> it, it will not be useful in the analysis; if I drop it, what if I
> want to check if "new" is correct or not ?
>
> I am looking forward to hearing how you deal with this kind of
> intermediate variables. Thank you!
>
> Amanda Fu
> On Tue, Dec 21, 2010 at 5:23 PM, Amanda Fu <mandy.fu1@gmail.com> wrote:
>> Hi all,
>>
>> 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!
>>
>> Sincerely,
>> Amanda Fu
>>
>
> *
> *   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/
>

--
Stas Kolenikov, also found at http://stas.kolenikov.name
Small print: I use this email account for mailing lists only.

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