Announcement

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

  • Extracting First Character in a String that Satisfies a Condition

    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
    }

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str6 string byte position str1 desired
    "2--3--" 1 "3"
    "2--3--" 3 "2"
    "K---7"  1 "K"
    "---3"   1 "" 
    end
    
    gen wanted=substr(ustrregexra(substr(string, 1, length(string)-position), "-", ""),-1, 1)
    Res.:

    Code:
    . l
    
         +--------------------------------------+
         | string   position   desired   wanted |
         |--------------------------------------|
      1. | 2--3--          1         3        3 |
      2. | 2--3--          3         2        2 |
      3. |  K---7          1         K        K |
      4. |   ---3          1                    |
         +--------------------------------------+

    Comment


    • #3
      Wow, such an elegant solution -- thank you so much Andrew for the kind help!

      Comment

      Working...
      X