Announcement

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

  • Removing observation that contain certain words

    I am trying to remove observations that contain several words, but I recieved a message about "invalid syntax". Can you please advise where the mistake is? I also tried dataex to list the observations but I recieved the following message: "data width (999 chars) exceeds max linesize. Try specifying fewer variables"

    The code I am trying currently is the following:

    Code:
    local wordlist "refines" "enhances" "flourishes" "matures" "expands" "grows"
    
    
    gen newvar = 1
    
    foreach w in wordlist' {
        replace newvar = 0 if subinstr(lower_KeyDevelopmentSituation, "`w'") != 0
    }
    
    drop if newvar == 0
    Last edited by Nick Baradar; 13 Feb 2023, 07:56.

  • #2
    looks like wordlist' should be foreach w in `wordlist' (notice the ` character). Also, subinstr() is used to incorrectly.

    You could also try

    Code:
    drop if ustrregexm(lower(lower_KeyDevelopmentSituation), "refines|enhances|flourishes|matures|expands|grows")
    Last edited by Justin Niakamal; 13 Feb 2023, 08:20.

    Comment


    • #3
      Originally posted by Justin Niakamal View Post
      looks like wordlist' should be foreach w in `wordlist' (notice the ` character). Also, subinstr() is used to incorrectly.

      You could also try

      Code:
      drop if ustrregexm(lower(lower_KeyDevelopmentSituation), "refines|enhances|flourishes|matures|expands|grows")
      Thank you very much, Justin! I just have too many words, I don't want to use all of them in the drop code; the 6 words that I provided are just for the sake of example. Could you please also help me by specifying what's wrong with the subinstr() function so that I can correct it?

      Thank you very much in advance!

      Comment


      • #4
        subinstr() is used for replacement. Eg subinstr(some_var, " ", "_", .) would replace all spaces with underscores.

        Try this for your problem

        Code:
        local wordlist   "refines" "enhances" "flourishes" "matures" "expands" "grows"
        
        foreach w in "`wordlist'" {
            drop if ustrregexm(lower(lower_KeyDevelopmentSituation), "`w'")
        }

        Comment


        • #5
          Originally posted by Justin Niakamal View Post
          subinstr() is used for replacement. Eg subinstr(some_var, " ", "_", .) would replace all spaces with underscores.

          Try this for your problem

          Code:
          local wordlist "refines" "enhances" "flourishes" "matures" "expands" "grows"
          
          foreach w in "`wordlist'" {
          drop if ustrregexm(lower(lower_KeyDevelopmentSituation), "`w'")
          }
          Thanks a lot for the explanation, Justin!
          I used to code that you suggested, but it deleted every observation I had, which cannot be true.
          Last edited by Nick Baradar; 13 Feb 2023, 09:44.

          Comment

          Working...
          X