Announcement

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

  • How to drop string "values" from a string variable with a loop (or any efficient tool)

    Hi everyone,

    I am doing data cleaning and I would like to drop a lot of string "values" from a string variable.
    I tried this expression with almost 100 strings, but the expression too long:

    Code:
    drop if inlist(description, "A.C.", "A.U.S.A.", "ABBEY", "AC CARS", "AC COBRA", "ACERBI", "ACI", "ACURA", "ADRIA", "ADRIA MOBIL", "ALKO-KOVER, ...")
    expression too long
    r(130);
    Could anyone give me a hint to do this in a better way, please?

    Thanks a lot!
    Michael

  • #2
    A local should to the trick, just note the enclosing `" "' since your data has embedded spaces:

    Code:
    local to_drop `" "A.C."  "A.U.S.A."  "ABBEY"  "AC CARS"  "AC COBRA"  "ACERBI" ..."'
    foreach val of local to_drop{
    drop if description == `val'
    }

    Comment


    • #3
      Hi Mike Murphy:

      Thank you so much for the suggestion!
      I obtain this when trying to replicate your code:

      Code:
      . local to_drop `" "A.C."  "A.U.S.A."  "ABBEY"  "AC CARS"  "AC COBRA"  "ACERBI" "'
      
      . foreach val of local to_drop{
        2. drop if description == `val'
        3. }
      A.C. invalid name
      r(198);
      Thank you for your help!
      Best,

      Michael

      Comment


      • #4
        I found. I think some " " are missing in `val':

        Code:
        local to_drop `" "A.C."  "A.U.S.A."  "ABBEY"  "AC CARS"  "AC COBRA"  "ACERBI" ..."'
        foreach val of local to_drop{
        drop if description == "`val'"
        }
        Everything works nicely now.
        Thank you so much for your help!
        Lovely day.

        Comment

        Working...
        X