Stata: string variable comparison

I have two string variables that differ by one character for each observation. I need to get the position of this other character. I tried to use the function indexnot(), but it gives false results, since the characters in both lines are the same. Here is an illustrative example, and the variable positionis the one I'm trying to jump to:

+--------------+--------------+-----------+
|   String 1   |   String 2   | Position  |
+--------------+--------------+-----------+
| 000002002000 | 000000002000 |         6 |
| 000002102000 | 000002002000 |         7 |
| 000002112000 | 000002102000 |         8 |
| 000002112020 | 000002112000 |        11 |
| 000002112120 | 000002112020 |        10 |
+--------------+--------------+-----------+
+4
source share
1 answer
gen Position = . 

quietly forval j = 1/12 { 
    replace Position = `j' if substr(String1, `j', 1) != substr(String2, `j', 1) & missing(Position)
} 

The comment here may be superfluous, but it will not hurt anyone.

, . a Position ( ). , 1 12, 12 . . , missing(Position) (Position == ., ) .

Stata , , .

+5

Source: https://habr.com/ru/post/1532709/


All Articles