Announcement

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

  • Extract info in parenthesis in string

    Hi there!

    I have a string variable in my data set which contains information in brackets (the brand) that I need in a separate vairable:

    VARIABLE
    Product1(Brand1)
    Product1(Brand2)
    Product 2(Brand 1)
    Product 2a(Brand 1)
    Product 45(Brand345)
    Product 62(Brand1)

    Since the length of the string is different, I guess I need to approach it over the parenthesis? Any idea how to extract what is in brackets? That would be fantastic!
    Best,
    Anna

  • #2
    You could use strpos() to search for parentheses (as () are better called) or a regular expression. Here I use moss (SSC) as a convenience wrapper.

    Code:
    clear
    input str42 whatever
     "Product1(Brand1)"
     "Product1(Brand2)"
     "Product 2(Brand 1)"
     "Product 2a(Brand 1)"
     "Product 45(Brand345)"
     "Product 62(Brand1)"
     end
     
    ssc inst moss
    moss whatever, match("\((.*)\)") regex
    
    l
    
         +--------------------------------------------------+
         |             whatever   _count    _match1   _pos1 |
         |--------------------------------------------------|
      1. |     Product1(Brand1)        1     Brand1      11 |
      2. |     Product1(Brand2)        1     Brand2      11 |
      3. |   Product 2(Brand 1)        1    Brand 1      12 |
      4. |  Product 2a(Brand 1)        1    Brand 1      13 |
      5. | Product 45(Brand345)        1   Brand345      13 |
         |--------------------------------------------------|
      6. |   Product 62(Brand1)        1     Brand1      13 |
         +--------------------------------------------------+
    See http://www.catb.org/jargon/html/A/ASCII.html for a helpful guide to character names

    Comment

    Working...
    X