Remove Text Within Delimeters

Accepted Solution Solved
Reply
Contributor
Posts: 32
Accepted Solution

Remove Text Within Delimeters

Hi there,

    I need to remove the brackets and text within the brackets from a string in SAS 9.2. I am having issues because some of my strings contain more than one bracketed string. Scan works great when there is only one set of brackets, but not when there are multiple sets.

 

Here is my sample data:

data have;

length char_var $25.;

input char_var $;

datalines;

Apple(MacIntosh)

4.99(Each)

(New)Ball2.0(Soccer)

(Ketchup)Chips

;

run;

 

I only need the strings that are not contained in brackets so I would like the end result to look like this:

CHAR_VAR

Apple

4.99

Ball2.0

Chips

 

I think I have to use prxmatch, but I am so unfamiliar with it that I cannot get it to work for me.

 

Thanks in advance.


Accepted Solutions
Solution
4 weeks ago
PROC Star
Posts: 311

Re: Remove Text Within Delimeters

Posted in reply to sas-inquirer

data have;
length char_var $30.;
input char_var & :$30.;
want = prxchange('s/\(([^\)]+)\)//i', -1, char_var);

datalines;
Apple(MacIntosh)
4.99(Each and Every)
(New)Ball2.0(Soccer and BBall)
(Ketchup)Chips
;
run;

View solution in original post


All Replies
PROC Star
Posts: 311

Re: Remove Text Within Delimeters

Posted in reply to sas-inquirer

prxchange will do it. This works based on your demo data, but you'll need to change the "\w..." portion if the data in the brackets has other characters, spaces, etc.

 

data have;
length char_var $25.;
input char_var $;
want = prxchange('s/\(\w{1,}\)//i', -1, char_var);
datalines;
Apple(MacIntosh)
4.99(Each)
(New)Ball2.0(Soccer)
(Ketchup)Chips
;
run;
Contributor
Posts: 32

Re: Remove Text Within Delimeters

[ Edited ]
Posted in reply to collinelliot

Thank you collinelliot! Your solution works great, but yes, I over simplified it and my actual data looks more like:

 

CHAR_VAR

Apple Red (MacIntosh Ontario)

Bear & Woods (Brown)

(New Today) Ball 2.0 (Soccer, other)

(Ketchup or Vinegar) Chips

 

When you noted I would need to change the /w portion, what would I need to change it to? Sorry I wasn't more clear in my example data.

Trusted Advisor
Posts: 1,615

Re: Remove Text Within Delimeters

Posted in reply to sas-inquirer

Try next not tested code:

flag=1;
do until (flag=0);
     ix1 = index(var,'(');
     if ix1 = 0 then flag=0; 
     else do;
        ix2 = index(var,')');
        if ix2 = 0 then put '>>> Right parenthesis is absent'; 
        else do;
            if ix1 =1 then var = substr(var,ix2+1);
            else if ix2 < length(trim(var)) 
               then var= substr(var,1,ix1-1) || substr(var,ix2+1);
               else var = substr(var,1,ix1-1);
       end;
    end;
end;
       
Contributor
Posts: 32

Re: Remove Text Within Delimeters

Thank you for replying Shmuel. I tried running the supplied code, but it puts ">>> Right parenthesis is absent" an incredible amount of times in the log. I keep having to terminate the process in order to make it stop. I'll be honest, the code looks so alien to me that I cannot even begin to figure out how to adjust it to make it work for me. Thank you for your time - this is just way above my head.

Trusted Advisor
Posts: 1,615

Re: Remove Text Within Delimeters

Posted in reply to sas-inquirer

@sas-inquirer, you may prefer work with prxchange function, anyhow the code I have supllied works fine.

Attached is the full code with addapting the variable name and adding one more row to the datalines:

data have;
length var $25.;
input var $;
***want = prxchange('s/\(\w{1,}\)//i', -1, char_var);
var_org = var;  /* keep original value */
flag=1;
do until (flag=0);
     ix1 = index(var,'(');    /* find open delimiter */
     if ix1 = 0 then flag=0;  /* no or no-more - open delimiters */
     else do;
        ix2 = index(var,')');  /* check for closing delimiter */
        if ix2 = 0 then put '>>> Right parenthesis is absent'; 
        else do;
             /**** choose how to substr the text to remove the delimited part ****/
            if ix1 =1 then var = substr(var,ix2+1);
            else if ix2 < length(trim(var)) 
               then var= substr(var,1,ix1-1) || substr(var,ix2+1);
               else var = substr(var,1,ix1-1);
       end;
    end;
end;
drop flag ix1 ix2;
datalines;
Apple(MacIntosh)
4.99(Each)
(New)Ball2.0(Soccer)
(Ketchup)Chips
Abc(added)def
;
run;
Respected Advisor
Posts: 4,973

Re: Remove Text Within Delimeters

Posted in reply to sas-inquirer

Change the pattern to

 

's/\([^)]*\)//io'

PG
Contributor
Posts: 32

Re: Remove Text Within Delimeters

Thank you PG - that worked wonderfully!

Solution
4 weeks ago
PROC Star
Posts: 311

Re: Remove Text Within Delimeters

Posted in reply to sas-inquirer

data have;
length char_var $30.;
input char_var & :$30.;
want = prxchange('s/\(([^\)]+)\)//i', -1, char_var);

datalines;
Apple(MacIntosh)
4.99(Each and Every)
(New)Ball2.0(Soccer and BBall)
(Ketchup)Chips
;
run;
Contributor
Posts: 32

Re: Remove Text Within Delimeters

Posted in reply to collinelliot

Thank you collinelliot! That did the trick. I really appreciate your time.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 185 views
  • 1 like
  • 4 in conversation