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.
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;
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;
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.
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;
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.
@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;
Change the pattern to
's/\([^)]*\)//io'
Thank you PG - that worked wonderfully!
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;
Thank you collinelliot! That did the trick. I really appreciate your time.
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.
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!
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.
Ready to level-up your skills? Choose your own adventure.