Announcement

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

  • Using strpos & "or" command

    Hello,

    I have a dataset where I want to keep string variables which include a certain word or another word or another and so on. The string variables "affiliatie" are names of Universities and I only want to keep those in the Netherlands and Belgium.

    Using : keep if strpos(affiliatie, "Erasmus" | "Antwerpen" | "Maastricht" etc etc (29 in total). gives the error type mismatch. Does anyone know how to solve this?

    Ciao,
    Chiara

  • #2
    You'd probably be better off with a loop. Something like the following could work.
    Code:
    generate byte keep = 0
    foreach affiliate in Erasmus Antwerpen Maastricht <etc.> {
        quietly replace keep = 1 if strpos(affiliatie, "`affiliate'") > 0
    }
    quietly keep if keep
    drop keep

    Comment


    • #3
      The | operator is an operator, not a command.

      Code:
      di strpos("toad", "a") | strpos("frog", "a")
      shows how it could be combined with the function strpos(), but for long lists of arguments go with Joseph's suggestion,

      or use http://www.stata.com/support/faqs/da...ets/index.html

      See also http://www.stata.com/support/faqs/da...ons/index.html

      Comment


      • #4
        Ok, thanx! I will try whether this works!

        Comment


        • #5
          Hello
          I have a dataset that contains a var named enumerator code. its contents are AN001 to AN300. some of the enumerator codes contains numbers like 200, 201 etc without AN.
          how do I sort it to extract the ones that does not have initial AN
          Thanks

          Comment


          • #6
            Code:
            *Some example data
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input str6 var1
            "AN001"
            "AN002"
            "AN200"
            "200"
            "300"
            end
            
            * Code
            sort var1
            gen hasAN = strpos(var1,"AN")
            gen var1_fixed =var1
            replace var1_fixed= "AN"+var1 if hasAN==0
            
            * Result
            
            . list
            
                 +--------------------------+
                 |  var1   hasAN   var1_f~d |
                 |--------------------------|
              1. |   200       0      AN200 |
              2. |   300       0      AN300 |
              3. | AN001       1      AN001 |
              4. | AN002       1      AN002 |
              5. | AN200       1      AN200 |
                 +--------------------------+

            Comment


            • #7

              Code:
              list if substr(whatever, 1, 2) != "AN"

              Comment


              • #8
                Right now the loop provided above does return all the values that have 'Erasmus' or 'Antwerpen' in it. So it includes 'ErasmusRotterdam' and 'ErasmusHironimos', etc..

                How am I able to change the code that it only returns the exact results 'Erasmus', 'Antwerpen', 'Maastricht' etc and not variations of them?


                Here the code from above: (sorry if it's not formatted perfectly. Still relatively new here)
                Code:
                 generate byte keep = 0 foreach affiliate in Erasmus Antwerpen Maastricht <etc.> {     quietly replace keep = 1 if strpos(affiliatie, "`affiliate'") > 0 } quietly keep if keep drop keep

                Comment


                • #9
                  Code:
                  generate byte keep = 0 
                  
                  quietly foreach affiliate in Erasmus Antwerpen Maastricht {     
                      replace keep = 1 if affiliatie == "`affiliate'"
                  } 
                  
                  keep if keep 
                  
                  drop keep

                  Comment


                  • #10
                    Thank you very much Nick. This worked.

                    Comment

                    Working...
                    X