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" <[email protected]>
To   <[email protected]>
Subject   st: RE: what does _N mean under by varlist in the -egen- and -gen-?
Date   Sat, 8 Aug 2009 15:30:08 +0200

<>
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: [email protected]
[mailto:[email protected]] On Behalf Of [email protected]
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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index