Announcement

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

  • Trying to delete informations inside the bracket on version 17.0

    Dear all Statist,

    I have a database which look like that :

    muni_id muni
    91530 PUERTO ALEGRÍA (Cor. Departamental)
    91263 EL ENCANTO (Cor. Departamental)
    91536 PUERTO ARICA (Cor. Departamental)
    91798 TARAPACÁ (Cor. Departamental)

    I'm trying to delete the expression in the bracket (with the brackets too) in the variable "muni" by using the command " replace muni = regexr(muni,"\(.+?\)","") " but it doesn't work at all.

    Do you have some clues to deal with this problem ?

    Thanks to all,

  • #2
    These work for me and for your data example. The first recipe hinges on finding the first (. and assumes that nothing of interest follows that, which could be wrong.

    The second recipe hinges on finding literal () within the string and deleting that and the characters in between. I can't follow what your regex is intended to do, but I can't see a way of doing this without parentheses.

    Code:
    clear
    input muni_id str42 muni
    91530 "PUERTO ALEGRÍA (Cor. Departamental)"
    91263 "EL ENCANTO (Cor. Departamental)"
    91536 "PUERTO ARICA (Cor. Departamental)"
    91798 "TARAPACÁ (Cor. Departamental)"
    end
    
    gen wanted = trim(substr(muni, 1, strpos(muni, "(") -1))
    
    gen WANTED = trim(regexr(muni, "\(.*\)", ""))
    
    list
    
         +---------------------------------------------------------------------------------+
         | muni_id                                  muni           wanted           WANTED |
         |---------------------------------------------------------------------------------|
      1. |   91530   PUERTO ALEGRÍA (Cor. Departamental)   PUERTO ALEGRÍA   PUERTO ALEGRÍA |
      2. |   91263       EL ENCANTO (Cor. Departamental)       EL ENCANTO       EL ENCANTO |
      3. |   91536     PUERTO ARICA (Cor. Departamental)     PUERTO ARICA     PUERTO ARICA |
      4. |   91798         TARAPACÁ (Cor. Departamental)         TARAPACÁ         TARAPACÁ |
         +---------------------------------------------------------------------------------+
    See http://catb.org/jargon/html/A/ASCII.html for some of the names in use. I tend to follow


    () parentheses
    [] brackets
    {} braces

    and if necessary add adjectives round, square, curly. but there are only two rules here that work.

    Principle. My usage of terms is careful and yours is too if you agree with me.

    Practice. Even people who think they are experienced computer users can be utterly clueless about what characters are called.
    Last edited by Nick Cox; 01 Feb 2023, 04:44.

    Comment


    • #3
      Thanks a lot Nick !

      Comment

      Working...
      X