Announcement

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

  • String: Extract first part of a string

    Hello guys,

    This may be a very easy questions but I couldn't figure it out. I have a string variable that looks something like this:

    MMDC-6MA.
    CC - GG55
    GMO - GU564

    I need to extract the first letters before the sign "-", So I need: GMDC , CC, GMO.


    Any ideas?

    I would greatly appreaciate it,
    Marvin

  • #2
    Marvin:

    Please use dataex (SSC)!
    Please use dataex (SSC)!
    Please use dataex (SSC)!

    ... even for simple examples.

    The following shows some technique.

    Code:
    clear 
    
    input str42 somemarvinstuff 
    "MMDC-6MA."
    "CC - GG55"
    "GMO - GU564"
    end 
    
    gen demo1 = substr(somemarvinstuff, 1, strpos(somemarvinstuff, "-") - 1) 
    gen demo2 = trim(demo1) 
    
    list 
    
         +-----------------------------+
         | somemarvi~f   demo1   demo2 |
         |-----------------------------|
      1. |   MMDC-6MA.    MMDC    MMDC |
      2. |   CC - GG55     CC       CC |
      3. | GMO - GU564    GMO      GMO |
         +-----------------------------+


    Step 1. Find the dash. How do you that? With a string function. How do you find the right one? Read help string functions.

    Step 2. You want whatever lies between position 1 and just before the dash. Then same advice as above.

    Step 3. Probably, the spaces are meaningless. If so, remove them. Then same advice as above.

    Comment


    • #3
      Thanks Nick! Will do!

      Comment

      Working...
      X