# st: RE: RE: what does _N mean under by varlist in the -egen- and -gen-?

 From "Martin Weiss" To Subject st: RE: RE: what does _N mean under by varlist in the -egen- and -gen-? Date Sat, 8 Aug 2009 15:56:34 +0200

```<>

The manuals make the problem clear, though: " Explicit subscripting (using
_N and _n), which is commonly
used with generate, should not be used with egen;" See [D], p. 145

HTH
Martin

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Martin Weiss
Sent: Samstag, 8. August 2009 15:30
To: statalist@hsphsun2.harvard.edu
Subject: st: RE: what does _N mean under by varlist in the -egen- and -gen-?

<>
You can of course form an expression that does yield the correct result:

***
clear
input family   sex
1     1
1     1
1     1
2     0
2     1
2     0
2     1
2     0
3     0
3     1
3     0
3     1
3     1
3     1

end

//complex expression fed to -total-
bys family: egen x=/*
*/total(sex*(sex[_N]==0))

//compare with -if- solution
bys family: egen xx=total(sex) /*
*/ if sex[_N]==0

list, noobs sepby(fam)
***

HTH
Martin

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of gjhxmu@sina.com
Sent: Samstag, 8. August 2009 11:40
To: statalist
Subject: st: what does _N mean under by varlist in the -egen- and -gen-?

Dear statalists,

I know that under by varlist: _N is interpreted within each group of
observations, not for the whole dataset.

However, I found the meaning _N in the -egen- is a little different.

Take the following for example,
clear
input family   sex
1     1
1     1
1     1
2     0
2     1
2     0
2     1
2     0
3     0
3     1
3     0
3     1
3     1
3     1

end
bys family: g x=sum(sex) if sex[_N]==0
bys family: egen xx=total(sex) if sex[_N]==0
bys family: g xxx=sex[_N]
bys family: egen xxxx=total(sex[_N]==0)
l,sepby(family) noobs

+------------------------------------+
| family   sex   x   xx   xxx   xxxx |
|------------------------------------|
|      1     1   .    .     1      0 |
|      1     1   .    .     1      0 |
|      1     1   .    .     1      0 |
|------------------------------------|
|      2     0   0    .     0      5 |
|      2     1   1    .     0      5 |
|      2     0   1    .     0      5 |
|      2     1   2    .     0      5 |
|      2     0   2    .     0      5 |
|------------------------------------|
|      3     0   .    .     1      0 |
|      3     1   .    .     1      0 |
|      3     0   .    .     1      0 |
|      3     1   .    .     1      0 |
|      3     1   .    .     1      0 |
|      3     1   .    .     1      0 |
+------------------------------------+

Based on the results, Obviously the meaning of _N in the first two is
different,
while the meaning of _N in the last two is the same.

Could anyone tell me why?

Thank you very much.

Best regards,
Rose.

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

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