gen Position = .
quietly forval j = 1/12 {
replace Position = `j' if substr(String1, `j', 1) != substr(String2, `j', 1) & missing(Position)
}
Commentary is perhaps redundant here, but will harm no-one.
In the absence of a built-in function to do this, you need to write some code using existing commands and functions. Initialise a Position
to missing (zero would do fine as an alternative). Then loop over the characters, here 1 to 12 because the example shows 12 character strings. We record the position of the first difference in characters. Note how the condition missing(Position)
(Position == .
if you like) restricts changes to the first difference met.
Stata loops automatically over all the observations here, so the only loop needed is over string positions.