Announcement

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

  • Wildcards with noccur

    Hi there,

    Can I use wildcards with the egen function noccur? If not, is there another way to use wildcards and count the number of occurence in one line of code?
    Thank you,

    Ylenia

  • #2
    -noccur()- is not an official Stata -egen- function; it is found in the -egenmore- package available at SSC. The help file there is clear that it takes only a single string variable as an argument. So unless the wildcard actually expands only to a single variable, it will produce a syntax error. I am not aware of any single command in Stata that would do what you ask for. I think you have no choice but to loop over the variables of the wildcard expansion, apply -egen, noccur()- to each, and add up the results.

    If you had numeric variables, the analogous problem would be handily solved with the -egen, anycount()- function. But that one only works with numeric variables, and looks for equality.

    Comment


    • #3
      I concur on noccur(). See also https://www.stata-journal.com/articl...article=dm0056


      For several variables, you need to flag whether you want to count how many variables contain a substring or whether you want to count how many occurrences there are of a substring in many variables.

      For example, if three variables have values


      Code:
      frog toad  Stata
      then

      1. two variables contain t

      2. three occurrences of t can be found across the variables.

      Which answer do you want? Either way, I think you need a loop and one should be easy so long as you clarify your precise goal.

      Comment


      • #4
        Thank you both for your answer, I realize now my question was not clear at all.
        Actually I want to use the wildcard inside the string, not to loop over several variables.
        Something like:

        egen nword1=noccur(text), string(whatev*)

        Comment


        • #5
          No, -egen, noccur()- looks for an exact match. It does not expand wildcards. If the expansion of whatev* is a list of variable names in the data in memory, then you can just expand the wildcard into a local macro using -unab- or -ds- and then use the local macro contents as the contents of the -string()- option. That would be a fairly simple two line solution.

          Comment

          Working...
          X