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

Hi

 

Is there a quick and easy way of searching and replacing a ( with an _

 

eg. %let x=Premium_Loans-44014(1);

%let y=%sysfunc(tranwrd(&x,"(","_"));

 

Rob

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
kiranv_
Rhodochrosite | Level 12

you can prxchange. \( meaning escape starting of paerenthesis and  \) is closing of parenthesis (\d) is meant to capture a number.

so \((\d)\) means replace (\d) capture d (digit) and replace () with _

 

 

 

%let x=%str(Premium_Loans-44014(1));
%put &x;
%let y=%sysfunc(prxchange(s/\((\d)\)/_\1/,1, &x));
%put &y; 


output is 

 73         %let x= Premium_Loans-44014(1);
 74         %put &x;
 Premium_Loans-44014(1)
 75         %let y=%sysfunc(prxchange(s/\((\d)\)/_\1/,1, &x));
 76         %put &y;
 Premium_Loans-44014_1

 

View solution in original post

9 REPLIES 9
ballardw
Super User

Is this what you are looking for?

rramcharan
Fluorite | Level 6

I can't see the code you are suggesting...sorry

Astounding
PROC Star

Once you replace "(" with "_" do you also want to remove the ")" ?

Astounding
PROC Star

Untested but should be workable:

 

%let part1 = %scan(%superq(macrovar), 1, %str(%)%());

%let part2 = %scan(%superq(macrovar), 2, %str(%)%());

 

The pieces in red are the way to refer to parentheses within the %STR function.

 

If you are certain that there will always be parentheses, you could simply use:

 

%let result = &part1._&part2;

 

If there might be parentheses just some of the time you would need to add a macro to be able to use %if / %then:

 

%if %length(&part2)=0 %then %let result = &part1;

%else %let result = &part1._&part2;

 

It's a little clumsy (and untested at this point), but it's difficult for macro language to use parentheses as part of the syntax since they need to be treated as both text and as significant characters.  

Reeza
Super User

Two other functions that may be useful:

 

SCAN() with () as the delimters to extract the portion before, within or after.

COMPRESS() which can remove the characters indicated and return the string without the characters.

 


@rramcharan wrote:

Hi

 

Is there a quick and easy way of searching and replacing a ( with an _

 

eg. %let x=Premium_Loans-44014(1);

%let y=%sysfunc(tranwrd(&x,"(","_"));

 

Rob

 

 


 

kiranv_
Rhodochrosite | Level 12

you can prxchange. \( meaning escape starting of paerenthesis and  \) is closing of parenthesis (\d) is meant to capture a number.

so \((\d)\) means replace (\d) capture d (digit) and replace () with _

 

 

 

%let x=%str(Premium_Loans-44014(1));
%put &x;
%let y=%sysfunc(prxchange(s/\((\d)\)/_\1/,1, &x));
%put &y; 


output is 

 73         %let x= Premium_Loans-44014(1);
 74         %put &x;
 Premium_Loans-44014(1)
 75         %let y=%sysfunc(prxchange(s/\((\d)\)/_\1/,1, &x));
 76         %put &y;
 Premium_Loans-44014_1

 

Tom
Super User Tom
Super User

If you always have the pattern xxxxx(xxx) then %SCAN() is probably the easiest.

160  %let x=Premium_Loans-44014(1);
161  %let y=%scan(&x,1,())_%scan(&x,2,());
162  %put &=y;
Y=Premium_Loans-44014_1
Ksharp
Super User
%let x=Premium_Loans-44014(1);
%put &x;
%let y=%sysfunc(translate(%bquote(&x),%str(_),%str(%()));
%put &y; 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 945 views
  • 1 like
  • 7 in conversation