Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: what does _N mean under by varlist in the -egen- and -gen-?
Date   Sat, 8 Aug 2009 14:34:15 +0200

<>

I can most easily explain (1), (3) and (4), but not (2). In (2), it does
indeed seem that the -if- qualifier is evaluated for the overall dataset,
and not with respect to the -by- groups. When I change the very last
observation in the -input- statement given by gjhxmu, a.k.a. Rose to "0",
the -total- statement all of a sudden does produce values instead of
missings...


**********
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     0 
  
end

bys family: egen x=/*
*/total(sex) if sex[_N]==0  

list, noobs sepby(fam)

replace sex=1 in l

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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index