# Re: st: Programming stata using egen functions

 From Richard Ochmann To statalist@hsphsun2.harvard.edu Subject Re: st: Programming stata using egen functions Date Tue, 28 Apr 2009 17:09:42 +0200

```...
not entirely clear what you want to do there,
but maybe you can borrow from this code on generating a list of scalars
(which might be inefficient either):

***
#delimit;
sysuse auto;
local meanlist = "0";
forvalues i = 1/2       {;
forvalues j = `i'/3     {;
quietly sum price, meanonly;
scalar s`i'`j' = r(mean)/runiform();
local meanlist = " `meanlist', `=s`i'`j'' ";
di "`meanlist'";
scalar mymax = max(`meanlist');
di mymax;
};
};
***

best, rich

owner-statalist@hsphsun2.harvard.edu schrieb am 28.04.2009 02:05:25:

> Dear All
> I am having some problems with my ado.file whenever I try to include
> some egen functions like rowmax and rowtotal. I am currently using
> Stata 10.0
>
>
>  program mymean, rclass
>   1.   syntax [varlist] [if]
>   2.   tokenize "`varlist'"
>   3.   forvalues s=9(1)20 {
>   4.   forvalues sp=9(1)`s' {
>   5.         qui summarize `1' if `sp'==u
>   6.         local mean1_`s'`sp' =r(mean)
>   7.         qui sum `1' if u==8
>   8.         local mean2_`s'`sp' =r(mean)
>   9.           scalar m_`s'`sp'=`mean1_`s'`sp'' + ((`mean1_`s'`sp'' -
> `mean2_`s'`sp'')/(`sp'-8))*(9-`sp')
>  10.         tempvar exem_`s' P1_`s' P_`s' ep_`s' ep2_`s'
>  11.         qui egen `exem_`s''=max(m_`s'`sp')
>  12.           return scalar exem2_`s'=`exem_`s''*1
>  13.           qui gen `P1_`s''=sum(P) if `s'==u & id==1
>  14.           qui egen `P_`s'' =max(`P1_`s'')
>  15.           scalar ep_`s'=exem2_`s'*`P_`s''
>  16.         qui gen `ep_`s''=exem2_`s'*`P_`s''
>  17.         return scalar eq29=sum(ep_`s')
>  18. }
>  19. }
>  20. end
> This program is running well. But in lines 11 and 17, I in fact need to
> use the commands rowmax rather than max and rowtotal rather sum. So
> when I use max and sum the program runs well but when I substitute this
> commands whith rowmax and rowtotal stata gives me the following error
> message after I typed mymean loghw:
>
>
>  program mymean, rclass
>   1.   syntax [varlist] [if]
>   2.   tokenize "`varlist'"
>   3.   forvalues s=9(1)20 {
>   4.   forvalues sp=9(1)`s' {
>   5.         qui summarize `1' if `sp'==u
>   6.         local mean1_`s'`sp' =r(mean)
>   7.         qui sum `1' if u==8
>   8.         local mean2_`s'`sp' =r(mean)
>   9.           scalar m_`s'`sp'=`mean1_`s'`sp'' + ((`mean1_`s'`sp'' -
> `mean2_`s'`sp'')/(`sp'-8))*(9-`sp')
>  10.         tempvar exem_`s' P1_`s' P_`s' ep_`s' ep2_`s'
>  11.         qui egen `exem_`s''=rowmax(m_`s'`sp')
>  12.           return scalar exem2_`s'=`exem_`s''*1
>  13.           qui gen `P1_`s''=sum(P) if `s'==u & id==1
>  14.           qui egen `P_`s'' =max(`P1_`s'')
>  15.           scalar ep_`s'=exem2_`s'*`P_`s''
>  16.         qui gen `ep_`s''=exem2_`s'*`P_`s''
>  17.         return scalar eq29=rowtotal(ep_`s')
>  18. }
>  19. }
>  20. end
> .
> end of do-file
> . mymean loghw
> r(111);
>
> So I would like to know why the program recognizes the max and sum egen
> functions but do not recognize (or do not find my variable) when I use
> the rowmax and rowtotal egen functions. Is there any programming
> differences between these two kind of egen functons?
> Thanks a lot
> Anne
>
> *
> *   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/

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