user2557039
user2557039

Reputation: 71

replace a variable's value if it contains a certain string

I have a variable called diagnosis and I want to replace everything that contains the word "pneumonia" with just "pneumonia"

I tried this:

replace diagnosis = "Pneumonia" if regexm (diagnosis, "pneumonia")  

But I got an error: unrecognized command: regexm

I have Stata/IC version 12.1 for Windows.

Upvotes: 0

Views: 4774

Answers (2)

user4690969
user4690969

Reputation:

Another approach.

. clonevar newdiag = diag

. replace newdiag = "pneumonia" if strpos(strlower(diag),"pneumonia")>0
(3 real changes made)

. list, clean noobs

                   diag     newdiag  
              pneumonia   pneumonia  
              Pneumonia   pneumonia  
    Bronchial pneumonia   pneumonia  
      Flu and pneumonia   pneumonia  
                earache     earache  

Upvotes: 0

Eric HB
Eric HB

Reputation: 887

Answer:

take out the space between regexm and (diagnosis, "pneumonia")

Additional suggestions:

regexm takes a long time, so I would do something more like

replace diagnosis = "Pneumonia" if diagnosis == "pneumonia"

which achieves the same result, or if you want to do this more generally you can write

replace diagnosis = strproper(diagnosis)

which has the same results in your example.

Upvotes: 1

Related Questions