SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
NewSASPerson
Quartz | Level 8
I would like to compress only items in the parenthesis as well as the parenthesis without affecting any place else. The code below gives me
 
The quick brown fox jumps over  lazy dogs. The quick brownfox jumps over the lazy dog.
but what I need is
The quick brown fox jumps over 23 lazy dogs. The quick brown-fox jumps over the lazy dog. 
 
data have;
text = "The quick brown fox jumps over 23 lazy dogs.(1-27) The quick brown-fox jumps over the lazy dog.(28)";
run;
 
data want;
set have;
text2 = compress(text,"()-1234567890");
run;
I would appreciate some help with this.
 
Thank you.
1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @NewSASPerson 

You can try to use a pearl regular expression:

data want;
	set have;
	text2=prxchange('s/\([\d-]+\)//i', -1, text);
run;

This looks for the pattern \([\d-]+\) in the variable 'text' as many times as needed (second argument = -1)

\([\d-]+\) = a parenthesis followed by one or more digits or hyphens followed by a parenthesis.

 

View solution in original post

3 REPLIES 3
ed_sas_member
Meteorite | Level 14

Hi @NewSASPerson 

You can try to use a pearl regular expression:

data want;
	set have;
	text2=prxchange('s/\([\d-]+\)//i', -1, text);
run;

This looks for the pattern \([\d-]+\) in the variable 'text' as many times as needed (second argument = -1)

\([\d-]+\) = a parenthesis followed by one or more digits or hyphens followed by a parenthesis.

 

NewSASPerson
Quartz | Level 8
Perfect! Thank you so much this worked.
ed_sas_member
Meteorite | Level 14

You're welcome @NewSASPerson !

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 3 replies
  • 1243 views
  • 1 like
  • 2 in conversation