BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sas-inquirer
Quartz | Level 8

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.

1 ACCEPTED SOLUTION

Accepted Solutions
collinelliot
Barite | Level 11

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

10 REPLIES 10
collinelliot
Barite | Level 11

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;
sas-inquirer
Quartz | Level 8

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.

Shmuel
Garnet | Level 18

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;
       
sas-inquirer
Quartz | Level 8

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.

Shmuel
Garnet | Level 18

@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;
PGStats
Opal | Level 21

Change the pattern to

 

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

PG
sas-inquirer
Quartz | Level 8

Thank you PG - that worked wonderfully!

collinelliot
Barite | Level 11

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;
sas-inquirer
Quartz | Level 8

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

gadnuk
Calcite | Level 5

This code works beautifully for my project as well. I'm trying to understand why.

 

Can someone explain to me what this part of the code is telling SAS: \(([^\)]+)\)?

 

Thank you.

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 6331 views
  • 1 like
  • 5 in conversation