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

 From gjhxmu@sina.com To statalist Subject st: what does _N mean under by varlist in the -egen- and -gen-? Date Sat, 08 Aug 2009 17:40:19 +0800

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