Announcement

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

  • Eliminate a part of a string

    Hello there,
    I would like to eliminate a part of a string starting from a position recorded in another variable for 2000 characters backward. I can't use dataex as the strings are very long.

    To give you an example let's say I want to eliminate the word BB (hence appearing at position 6) from the string text and the 3 characters before

    text: AAAA BBC D
    Bpos: 6
    Wanted: AAC D

    Thank you,

    Ylenia
    Last edited by Ylenia Curci; 29 Oct 2021, 15:59.

  • #2
    I don't understand the question. How do we know that it is BB rather than BBC or "BBC " or "BBC D" that is to be eliminated? Is that in some other variable in your data set? Or is it BB that is to be removed in every observation? Similarly, is it always position 2000 where we should look for this? Or is that, also, given in some variable in the data set? And, finally, what do you want to do if "BB" isn't actually found at that position?

    Comment


    • #3
      Perhaps this example of deleting characters 3-5 from an 8-character string will start you in a useful direction.
      Code:
      . generate text = "ABCDEFGH"
      
      . * eliminate charcters in positions 3-5 (CDE)
      . generate wanted = substr(text,1,3-1)+substr(text,5+1,.)
      
      . list, clean
      
                 text   wanted  
        1.   ABCDEFGH    ABFGH  
      
      .

      Comment


      • #4
        Thank you William, this works perfectly.

        Comment

        Working...
        X