Announcement

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

  • Replacing variable if other variable contains certain word

    Hello!

    I want to replace a variable if another variable contains a certain word, e.g. if variable "position" says "Chief Marketing Officer", I want to replace the variable "functional"==1 (due to searching for "marketing").

    How can I do this?

    Thank you in advance

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str35 text float functional
    "Chief Marketing Officer"       0
    "Chief Financial Officer"       0
    `" "Marketing" consultant"'     0
    "National account sales executive" 0
    "Executive (MARKETING)"         0
    end
    
    
    replace functional =regexm(" " + lower(text) + " ", `"['!?,\.\(\) " ](marketing)['!?,\.\(\) " ]"') if !functional
    Res.:

    Code:
    . replace functional =regexm(" " + lower(text) + " ", `"['!?,\.\(\) " ](marketing)['!?,\.\(\) " ]"') if !functional
    (3 real changes made)
    
    . l
    
         +---------------------------------------------+
         |                             text   functi~l |
         |---------------------------------------------|
      1. |          Chief Marketing Officer          1 |
      2. |          Chief Financial Officer          0 |
      3. |           "Marketing" consultant          1 |
      4. | National account sales executive          0 |
      5. |            Executive (MARKETING)          1 |
         +---------------------------------------------+

    Comment


    • #3
      Thank you very much! How can I insert the number I want to replace functional with? E.g. if I want to replace it with a 2 if it contains "financial"?

      Comment


      • #4
        The replace statement presumably starts

        Code:
        replace functional = 2 if
        and the regexm() call needs to be combined with the other condition.

        Comment

        Working...
        X