Announcement

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

  • Replacing string character in specific position in the variable

    Hello, everyone.

    I am sorry if I repeated the same topic with, someone did.
    I have a problem in replacing string character in a specific position. Let say; I have a variable ID which its type is the string and 10 char long.

    Code:
    ID
    0013003001
    0013004001
    0023001003
    0033001003
    0033003003
    I want to make a new variable that consists of ID variable value but replacing the "3" character in coordinate position (3,1) only by "1" character . If I could make it, the new variable will become like this:

    Code:
    ID_new
    0011003001
    0011004001
    0021001003
    0031001003
    0031003003
    How to make it using a command in stata?
    Thank you.


  • #2
    I have worked to reproduce your example. I can't make sense of "coordinate position (3, 1)" as a description of what you want.

    Code:
    clear 
    input str10 ID
    0013003001
    0013004001
    0023001003
    0033001003
    0033003003
    end 
    
    gen ID_new = substr(ID, 1, 3) + "1" + substr(ID, 5, .) 
    
    list 
    
    
         +-------------------------+
         |         ID       ID_new |
         |-------------------------|
      1. | 0013003001   0011003001 |
      2. | 0013004001   0011004001 |
      3. | 0023001003   0021001003 |
      4. | 0033001003   0031001003 |
      5. | 0033003003   0031003003 |
         +-------------------------+

    Comment


    • #3
      Solved by your help Nick. Thank you so much.

      Comment

      Working...
      X