Announcement

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

  • Counting amount of times a specific value occurs in a variable

    Hi, I am trying to calculate the number of times a specific value (in this case a string) occurs in a variable, and place it in a new variable.
    Code:
    gen occurence_id = .
    In the end I would like it to be like this:
    id occurence_id
    12345678 4
    45624845 1
    12345678 4
    51564215 3
    56435158 1
    12345678 4
    46548675 1
    51564215 3
    12345678 4
    51564215 3
    Any ideas on how I can do this?


    Kind regards,
    Petar

  • #2
    It is hard to see why that specific output would be useful, but consider

    Code:
    gen long obsno = _n 
    
    bysort id : gen freq = _N 
    
    sort obsno 
    
    list id freq
    where I'd recommend just the second command.

    Comment

    Working...
    X