BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ilikesas
Barite | Level 11

Hi,

suppose I have a table with several columns:

nameinfo1info2
ABC (gfd)ab
DEF ( h  ffdds)cd
Zef

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:

nameinfo1info2
ABCab
DEFcd
Zef

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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;

View solution in original post

10 REPLIES 10
art297
Opal | Level 21

data want;

  set have;

  name=scan(name,1,'(');

run;

ilikesas
Barite | Level 11

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

art297
Opal | Level 21

Mostly, but I would say that it extracts all of the characters before the first '('.

ilikesas
Barite | Level 11

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

art297
Opal | Level 21

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;

ilikesas
Barite | Level 11

Worked!!!

Thank a lot!!

Ksharp
Super User

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

ilikesas
Barite | Level 11

Thanks xia keshan, I wish I could put more than one correct answer!!

RamKumar
Fluorite | Level 6

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.

Ksharp
Super User

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

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.

Discussion stats
  • 10 replies
  • 1284 views
  • 6 likes
  • 4 in conversation