Announcement

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

  • Susbtituting characters or numbers inside a variable

    Dear colleagues,:

    I have to work with a database that contains codes that I would like to codify. In order to do that I would like to substituite the certain values inside the variable.

    For example:

    AD00053
    zoC0151

    I would like to add at the beginning the characters CL1 and afterwards substitute the letter A for 1 D for 4 and then then the numbers by order per oder numbers. This should be done with a loop, since I need to do it over and over all the observations (+200.000)

    Thank you very much for your help

  • #2
    No, you don't need a loop to do something to all the observations. In Stata and all similar programs, a command like this applies to *all* the observations unless specifically limited to a subset. I don't understand what you mean by: "then the numbers by order per oder numbers." However, I can show you some technique and point you to what part of the help files to read to learn about this sort of thing:

    Suppose your string variable is named "S." You might do this
    Code:
    replace S = "CL1" +  S 
    replace S = subinstr(S, "1", "A", .)  
    replace S = subinstr(S, "4", "D",')
    Suggested help reading:
    -help operators- (about string concatenation)
    -help string functions- (about the many ways to modify a string variable.)

    Comment

    Working...
    X