Announcement

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

  • Convert from String to String

    Hello Statlist Users,
    First time here !!!. I am currently working on my research but I am new to Stata.

    Please how do I convert a String Variable to another string variable? I have a string variable with some states in the USA. I want to change those states to just USA.

    For example, I don't want it to show Florida, Washington, etc I want it to just be USA for all USA states.

    Thanks

  • #2
    if you had followed the advice in the FAQ, exact code could have been provided so please read the FAQ; you want to do something like:
    Code:
    replace x="USA"
    where you replace "x" with the actual variable name; see
    Code:
    h replace

    Comment


    • #3
      Thank you Rich for your response.
      To further explain my question, I was hoping there is a quicker way to replace the observations within my variable.

      For example: replace Origin = “USA” if Origin = “GA”, "WA", "VA"

      I have also tried the "substr" code but it only changes the first observation mentioned (in this case GA and ignores the rest).

      Is it possible to have multiple state observations changed at once within my variable?

      Thank you.

      Comment


      • #4
        Do you have state names or 2 letter state abbreviations? If the latter:

        Code:
        frame create states
        frame states: sysuse census, clear
        frame states: levelsof state2, local(states) sep(|) clean
        replace Origin= "USA" if regexm(trim(Origin), "(`states')")
        frame drop states

        Comment

        Working...
        X