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
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
Is this what you are looking for?
I can't see the code you are suggesting...sorry
Once you replace "(" with "_" do you also want to remove the ")" ?
yes
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.
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
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
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
%let x=Premium_Loans-44014(1);
%put &x;
%let y=%sysfunc(translate(%bquote(&x),%str(_),%str(%()));
%put &y;
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: