Announcement

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

  • Beginner: Frequency of variable in dataset

    I tried a few egen commands to do what I need, but can't figure it out.

    I want to create a new variable that returns a total by each unique value in the the "country" column.

    EX:

    have:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str5 country float
    "USA"  
    "USA"  
    "USA"  
    "China"
    "China"
    "USA"  
    "USA"  
    "USA"  
    "China"
    "China"
    end
    want:
    country countryfreq
    USA 6
    USA 6
    USA 6
    USA 6
    China 4
    China 4
    China 4
    China 4
    USA 6
    China 4

  • #2
    By “unique” I think you mean “dIstinct”.

    Code:
    bysort country : gen freq = _N
    is perhaps the simplest Stataish way.

    Code:
    egen freq = total(1), by(country)
    would also work.

    Comment


    • #3
      Thanks!

      Comment

      Working...
      X