Announcement

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

  • replacing characters with another in string variable

    Hi all, how can replace characters in a string variable with another character? For example, I need to change 6-090/123/123p to 6_090_123_123p to facilitate a merge across two data sets. It seems so simple but nothing I try has seemed to work!
    Thanks in advance for you help

  • #2
    Well, it depends on how general your example is. Suppose the variable you have is named my_var.
    Code:
    replace my_var = subinstr(myvar, "/", "_", .)
    replace my_var = subinstr(myvar, "-", "_", .)
    will do it. If, however, there are other replacements that need to be done in other values of the variable, then other code would need to be written for that. As you provide no example data, the best one can do at this point is speculate.

    If the above code is not a complete solution, please post back and be sure to show representative example data that illustrates the full breadth of your problem. The most helpful way to show example data is by using the -dataex- command. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment

    Working...
    X