Hello,

My data contains in each row:

(1) a string variable of different lengths ("String")
(2) a number corresponding to a specific character in that string ("Position")

For instance, a value of "1" for Position refers to the last character in the string, "2" is the second-last character, and so on.


What I would like to do is identify the most proximal character in each string that is:

- to the left of the character identified by the position number AND
- not equal to the dash symbol ("-")

The table below illustrates in the column "Desired" what character I would like to extract:

String Position Desired
2--3-- 1 3
2--3-- 3 2
K---7 1 K
---3 1

I tried the below code to begin at the original position of each string and "step" to the left of the string until there is a non-dash. However, the "step" part seems to fail. Any help would be much appreciated -- thank you very much!

Code:
local success=0
local i=1

while `success' < 1 {
    
    local char substr(String, (-1)*Position-`i',1) // get one character to the left of starting position
    
    replace Desired= `char' if `char' != "-" //fill in character if not a dash
    local i= `i' + 1 //move index by one
    
    local success= cond(`char' != "-", 1, 0) //success equals 1 when character not a dash
}