Announcement

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

  • Create string variable that copies all string letters from other variable up to specific letters

    Hi

    I would like to create a string variable that copies all characters from another string variable up to specific characters:

    Here is a sample of the data:
    Code:
    MarketName
    1414 Degrees Limited CONS 285@31 19M13:733751~1072
    1414 Degrees Limited CONS [email protected] 19M13:733745~875
    8IP Emerging Companies Limited CONS 125@71 19M13:733746~2001
    8IP Emerging Companies Limited CONS 126@72 19M13:733746~1995
    8common Limited CONS [email protected] 19M13:733745~532
    8common Limited CONS [email protected] 19M13:733745~531
    9 Spokes International Limited CONS [email protected] 19M13:774955~1983
    9 Spokes International Limited CONS [email protected] Z8750-BL150442
    9 Spokes International Limited Rights Issue - Taken Up CONS [email protected] Z8750-BL147287
    9 Spokes International Limited Rights Issue - Taken Up CONS [email protected] Z8750-BL150434
    I am trying to generate another string variables up to the characters " CONS ". For the given data sample, that would be:
    Code:
    MarketName
    1414 Degrees Limited
    1414 Degrees Limited
    8IP Emerging Companies Limited
    8IP Emerging Companies Limited
    8common Limited
    8common Limited
    9 Spokes International Limited
    9 Spokes International Limited
    9 Spokes International Limited Rights Issue - Taken Up
    9 Spokes International Limited Rights Issue - Taken Up
    I have tried using a combination of the following, but I could not reach my goal:
    gen newvar = substr(MarketName, strpos(MarketName, ":") + 1, .) Please help. Thanks!

  • #2
    Thanks for a clear example showing what you want.. The syntax you tried is in the right direction, but strpos() should be looking for "CONS" and not ":". Also, you indicate to substr() that you wanted *all* of the rest of the string, but you only want the first part. Try this:

    Code:
    gen newvar = substr(MarketName, 1, strpos(MarketName, "CONS") -1)

    Comment


    • #3
      Mike Lacy gave a fine answer. Here is another way to do it:

      Code:
      split MarketName, p(CONS) limit(1)

      Comment


      • #4
        Thanks a lot to both of you!

        Comment

        Working...
        X