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.

# Re: st: -egen total()- function and indexing within the -if- condition

 From Nick Cox To "statalist@hsphsun2.harvard.edu" Subject Re: st: -egen total()- function and indexing within the -if- condition Date Mon, 15 Apr 2013 12:19:38 +0100

```Whatever you see here is not a bug. The help for -egen- is explicit:

"Explicit subscripting (using _N and _n), which is commonly used with
-generate-, should not be used with -egen-"

and that that applies to your syntax too. -egen- feels free to sort
values temporarily and that makes any kind of subscripting unreliable.

I'd assert that there is always a way round this, depending on what
the real problem is.

Nick
njcoxstata@gmail.com

On 15 April 2013 12:03, A Loumiotis <antonis.loumiotis@gmail.com> wrote:
> Hi,
>
> I thought that the index number in the -if- condition is reseted to 1
> at the beginning of every -by- group when generating a variable using
> the -egen total()- function.  It seems that this is not the case
> though as the example below demonstrates:
>
> ********
> clear
> input int i j x
> 1 1 10
> 1 2 0
> 1 3 15
> 2 1 15
> 2 2 1
> 2 3 20
> end
> bys i:gen s2x=sum(x) if x[2]==1 & inlist(j,1,3)
> bys i:egen t2x=total(x) if x[2]==1 & inlist(j,1,3)
> bys i:gen s5x=sum(x) if x[5]==1 & inlist(j,1,3)
> bys i:egen t5x=total(x) if x[5]==1 & inlist(j,1,3)
> li, sepby(i)
>
>      +------------------------------------+
>      | i   j    x   s2x   t2x   s5x   t5x |
>      |------------------------------------|
>   1. | 1   1   10     .     .     .    25 |
>   2. | 1   2    0     .     .     .     . |
>   3. | 1   3   15     .     .     .    25 |
>      |------------------------------------|
>   4. | 2   1   15    15     .     .    35 |
>   5. | 2   2    1     .     .     .     . |
>   6. | 2   3   20    35     .     .    35 |
>      +------------------------------------+
>
> *********
>
> I expected that t2x and t5x would be missing whenever s2x and s5x is
> missing but it is not.  The results of the -sum()- function seems
> logical to me given the -if- condition but not the results of the
> -total()- function.  Is this the normal behavior of the -total()-
> function or is this a bug?
>
> Best,
> Antonis
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```