Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • _n not working as a running counter within a by-group

    Dear Statalist,

    I normally use _n within a by-group as the running counter within the group. Examples of this are option 1 and 2. However, it appears not to be working in option 3 as it should. It seems to me it should give same results as option 1 but it appears that the 'max' function is taking the running counter of the complete database. I include option 2 as to show this is not the case for other functions in egen.

    Any ideas?

    Code:
    clear
    set obs 10
    gen group=ceil(_n*2/_N)
    
    gen n=_n
    sort group n
    
    *Option 1
    by group: gen n1=_n //Works as running counter within a by-group
    by group: egen o1=max(n1)
    
    *Option 2:
    by group: egen o2=mean(_n) //Works as running counter within a by-group
    
    *Option 3:
    by group: egen o3=max(_n) //Works as running counter within the complete dataset

    HTML Code:
         +--------------------------------+
         | group    n   n1   o1   o2   o3 |
         |--------------------------------|
      1. |     1    1    1    5    3    5 |
      2. |     1    2    2    5    3    5 |
      3. |     1    3    3    5    3    5 |
      4. |     1    4    4    5    3    5 |
      5. |     1    5    5    5    3    5 |
      6. |     2    6    1    5    3   10 |
      7. |     2    7    2    5    3   10 |
      8. |     2    8    3    5    3   10 |
      9. |     2    9    4    5    3   10 |
     10. |     2   10    5    5    3   10 |
         +--------------------------------+
         

    Thanks!

    Francisco
    Last edited by Francisco Pardo; 18 Oct 2018, 18:16.
Working...
X