Announcement

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

  • Check if string variable values are in a set of strings stored in macro

    I have a string variable and want to do a comparison to see if its values match with any of a set of strings stored in another macro.

    Here is an example somewhat close to what I am thinking of...



    Code:
    sysuse auto, clear
    levelsof make in 10/12, local(Names)
    gen FlagMember = (make == `Names')
    The expected output is generating a 1 for observations 10, 11, 12. Howwever it only generates a 1 for observation 10.

    Thanks for reading.

  • #2
    You need to check for the presence of each string in your list of strings. (make == `Names') checks whether the variable make is equal to the entire content of Names, which for various reasons will not be true.

    Code:
    clear
    sysuse auto, clear
    levelsof make in 10/12, local(Names)
    gen FlagMember = 0
    foreach n of local Names {
       replace FlagMember = 1 if (make == "`n'")
    }
    There are also possibilities using the function -inlist()- but that is harder than a small loop here.

    Comment


    • #3
      Thank you Mike. Do you know, is there not a single statement kind of command for "any match" or "in" ? I was hoping so since that would look a little sleeker but this will also of course do the job, thanks again for your help.

      Comment


      • #4
        You can do this if you use the inlist() function. You need to use the separate option in the levelsof command to generate a macro that contains a comma separated list. Note that the inlist() function has very restrictive limits, in particular for strings. From the help file:

        The number of arguments is between 2 and 255 for reals and between 2 and 10 for strings.
        So this would look like:
        Code:
        sysuse auto, clear
        levelsof make in 10/12, local(Names) separate(,)
        gen FlagMember = inlist(make,`Names')
        I assume that you are doing this to check for matches in other variables. That's usually done in Stata with merge. Here's a quick example:

        Code:
        sysuse auto, clear
        gen make2 = make[_n-1]
        
        * make a dataset of unique make names in 10/12
        preserve
        keep in 10/12
        keep make
        bysort make: keep if _n == 1
        rename make make2match
        save "my_names.dta", replace
        restore
        
        clonevar make2match = make2
        merge m:1 make2match using "my_names.dta"
        gen FlagMember = _merge == 3
        drop _merge
        sort foreign make

        Comment


        • #5
          Thank you Robert, that inlist functions looks perfect! Except for the limit with strings, of course, which seems bizarre and quite unfortunate. I didn't think about merge for this purpose but I see what you mean that will also do the job, thanks very much.

          Comment

          Working...
          X