Hi,
suppose I have a table with several columns:
name | info1 | info2 |
---|---|---|
ABC (gfd) | a | b |
DEF ( h ffdds) | c | d |
Z | e | f |
What I would like to do if possible is to delete the parentheses for the name column entries and all that is inside those parentheses, and note that the length of the part that I want to be deleted varies from entry to entry, and I have many rows... And the entries without the parentheses keep as is
So the end result should be like this:
name | info1 | info2 |
---|---|---|
ABC | a | b |
DEF | c | d |
Z | e | f |
Thank you!
That is what I would do:
data have;
informat name $25.;
input name &;
cards;
ABC (fjfj fjfj l ) DEF
;
data want;
set have;
name=catt(scan(name,1,'('),scan(name,-1,')'));
run;
data want;
set have;
name=scan(name,1,'(');
run;
Thanks Dr for the quick reply, it worked perfectly!!!
I guess that the meaning of the code is that it deletes for a given entry everything after and including the first ( and returns the entry in its new form
Mostly, but I would say that it extracts all of the characters before the first '('.
And what if the parentheses are in the middle of the entry, like:
ABC (fjfj fjfj l ) DEF
and the result that we want to get if ABC DEF ?
Does the code for this require 2 scans and a concatenation?
Thank you
That is what I would do:
data have;
informat name $25.;
input name &;
cards;
ABC (fjfj fjfj l ) DEF
;
data want;
set have;
name=catt(scan(name,1,'('),scan(name,-1,')'));
run;
Worked!!!
Thank a lot!!
Perl Regular Expression Version :
data have; informat name $25.; input name &; cards; ABC (gfd) ABC (fjfj fjfj l ) DEF DEF ( h ffdds) ; data want; set have; name=prxchange('s/\(.*\)//o',-1,name); run;
Xia Keshan
Thanks xia keshan, I wish I could put more than one correct answer!!
What if we've '%' around characters? e.g. ABC %fjfj fjfj l% DEF
I need the output as ABC DEF. I'm curious to know the perl regular expression version for this case.
OK. Here is . Peal Regular Expression \(.*\)|%.*% just match the pattern (fjfj fjfj l ) or %fjfj fjfj l% , you cold check it more in the sas documentation.
data have; informat name $25.; input name &; cards; ABC (gfd) ABC (fjfj fjfj l ) DEF DEF ( h ffdds) ABC %fjfj fjfj l% DEF ; data want; set have; name=prxchange('s/\(.*\)|%.*%//o',-1,name); run;
Xia Keshan
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.