Announcement

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

  • Using regular expressions to recode string variable

    Hello Statalist,

    I am recoding text responses to a survey question and I would like to tell Stata to recode answers starting with "Demi" to a particular category. I am not sure how to do this with string variables. I have successfully done this before with string variables using a command like this "destring v*, force replace", but do not know how to do this with the contents of the variable.

    Below is an example of the piece of code I am struggling with:
    Code:
    replace gendercatx=5 if dem_genderid_6_TEXT=="Genderfluid" | dem_genderid_6_TEXT=="Demi+" | dem_genderid_6_TEXT=="genderfluid"
    I am also curious if there is an easy way to tell Stata to ignore capitalization? For example, I have responses such as "Agender" and "agender" that I want to recode to the same category.

    Thanks,
    Laura

  • #2
    Code:
     
     replace gendercatx=5 if inlist(lower(dem_genderid_6_TEXT), "genderfluid", "demi+", "agender")
    Warning: no more than 10 distinct strings as arguments in a single inlist() call.

    Comment


    • #3
      Thank you Nick! Will this code include the capitalized version of these terms, such as "Demi+", "Agender", etc?

      Comment


      • #4
        Should do. Did you try it? Sometimes the best way to test code is to create a small sandbox dataset encapsulating the main problem,

        Comment

        Working...
        X