Announcement

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

  • how to create a table of frequencies for a local list of words

    Hi, suppose I have a local list, e.g.,

    local mylist "equity allocation equity equity allocation bond"

    Note that the word "equity" occurs three times, "allocation" twice, "bond" once.

    How do I create a table of frequencies, e.g.,

    equity 3
    allocation 2
    bond 1

    Thanks so much for your help!

    Best,


    John

  • #2
    Code:
    local mylist "equity allocation equity equity allocation bond"
    clear
    set obs `=wordcount("`mylist'")'
    g which=""
    forval i=1/`=_N'{
        qui replace which=word("`mylist'", `i') in `i'
    }
    contract which
    Res.:

    Code:
    . l
    
         +--------------------+
         |      which   _freq |
         |--------------------|
      1. | allocation       2 |
      2. |       bond       1 |
      3. |     equity       3 |
         +--------------------+

    Comment


    • #3
      Dear Andrew,

      Thanks so much for the code! Works like a charm!

      Best,


      John

      Comment


      • #4
        If you have more than a single macro, then various options are discussed in this thread: https://www.statalist.org/forums/for...nd-inefficient
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment

        Working...
        X