Announcement

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

  • Count number of different values with group-command

    Hello members,

    I would like to find out how many different values there are for the variable betnr (establishment number) per year.
    To do this, I used the command "egen count_betnr = group(betnr year)" to count the number of different establishment numbers per year.
    However, I then get different numbers for the same year. How can this be explained?

    Is there another approach to count the number of different establishment numbers per year?

  • #2
    See e.g.

    Code:
    SJ-23-2 dm0042_4  . . . . . . . . . . . . . . . . Software update for distinct
            (help distinct, distinctgen if installed)  N. J. Cox and G. M. Longton
            Q2/23   SJ 23(2):595--596
            most important change is addition of distinctgen command
    
    SJ-20-4 dm0042_3  . . . . . . . . . . . . . . . . Software update for distinct
            (help distinct if installed)  . . . . . .  N. J. Cox and G. M. Longton
            Q4/20   SJ 20(4):1028--1030
            sort() option has been added
    
    SJ-15-3 dm0042_2  . . . . . . . . . . . . . . . . Software update for distinct
            (help distinct if installed)  . . . . . .  N. J. Cox and G. M. Longton
            Q3/15   SJ 15(3):899
            improved table format and display of large numbers of
            observations
    
    SJ-12-2 dm0042_1  . . . . . . . . . . . . . . . . Software update for distinct
            (help distinct if installed)  . . . . . .  N. J. Cox and G. M. Longton
            Q2/12   SJ 12(2):352
            options added to restrict output to variables with a minimum
            or maximum of distinct values
    
    SJ-8-4  dm0042  . . . . . . . . . . . .  Speaking Stata: Distinct observations
            (help distinct if installed)  . . . . . .  N. J. Cox and G. M. Longton
            Q4/08   SJ 8(4):557--568
            shows how to answer questions about distinct observations
            from first principles; provides a convenience command
    where the 2008 paper remains the fullest discussion (that I know of) and contains a simple two-step using official code only:

    Code:
    egen tag = tag(betnr year)
    egen distinct = total(tag), by(year)
    followed by e.g.

    Code:
    tabdisp year, c(distinct)
    This is equivalent:

    Code:
    bysort year (betnr) : gen wanted = sum(betr != betr[_n-1]) 
    by year : replace wanted = wanted[_N]

    Comment


    • #3
      Thank you Nick!

      Comment

      Working...
      X