I am working on some RTF code generation. I am using following code that is working fine but i am not well in PRX functions.
Is there any way to simplyfy my code?
Code:
data _null_;
length tmptxt $100.;
tmptxt="a\b Refer $crossref(Table 1) and $italic.Section $crossref(3.1)$enditalic";
tmptxt=prxchange('s/([\\{}])/\\$1/',-1,tmptxt);
tmptxt= prxchange('s/([\$][a-zA-Z]+)([\(])/%$1$2/',-1,tmptxt);
tmptxt=prxchange('s/%\$/%/',-1,tmptxt);
tmptxt=prxchange('s/([\$][a-zA-Z]+)/&$1$2/',-1,tmptxt);
tmptxt=prxchange('s/&\$/&/',-1,tmptxt);
put tmptxt;
run;
Thanks in advance
You can reduce the number of PRXCHANGE functions by collapsing the two-step changes you are making for $->% and $->&
data _null_;
length tmptxt $100.;
tmptxt="a\b Refer $crossref(Table 1) and $italic.Section $crossref(3.1)$enditalic";
tmptxt=prxchange('s/([\\{}])/\\$1/',-1,tmptxt);
/*
tmptxt=prxchange('s/([\$][a-zA-Z]+)([\(])/%$1$2/',-1,tmptxt);
tmptxt=prxchange('s/%\$/%/',-1,tmptxt);
*/
tmptxt=prxchange('s#[\$]([a-z]+)(?=\()#%$1#i',-1,tmptxt);
/*
tmptxt=prxchange('s/([\$][a-zA-Z]+)/&$1$2/',-1,tmptxt);
tmptxt=prxchange('s/&\$/&/',-1,tmptxt);
*/
tmptxt=prxchange('s#[\$]([a-z]+)#&$1#i',-1,tmptxt);
put tmptxt;
run;
With comments removed:
data _null_;
length tmptxt $100.;
tmptxt="a\b Refer $crossref(Table 1) and $italic.Section $crossref(3.1)$enditalic";
tmptxt=prxchange('s/([\\{}])/\\$1/',-1,tmptxt);
tmptxt=prxchange('s#[\$]([a-z]+)(?=\()#%$1#i',-1,tmptxt);
tmptxt=prxchange('s#[\$]([a-z]+)#&$1#i',-1,tmptxt);
put tmptxt;
run;
You can reduce the number of PRXCHANGE functions by collapsing the two-step changes you are making for $->% and $->&
data _null_;
length tmptxt $100.;
tmptxt="a\b Refer $crossref(Table 1) and $italic.Section $crossref(3.1)$enditalic";
tmptxt=prxchange('s/([\\{}])/\\$1/',-1,tmptxt);
/*
tmptxt=prxchange('s/([\$][a-zA-Z]+)([\(])/%$1$2/',-1,tmptxt);
tmptxt=prxchange('s/%\$/%/',-1,tmptxt);
*/
tmptxt=prxchange('s#[\$]([a-z]+)(?=\()#%$1#i',-1,tmptxt);
/*
tmptxt=prxchange('s/([\$][a-zA-Z]+)/&$1$2/',-1,tmptxt);
tmptxt=prxchange('s/&\$/&/',-1,tmptxt);
*/
tmptxt=prxchange('s#[\$]([a-z]+)#&$1#i',-1,tmptxt);
put tmptxt;
run;
With comments removed:
data _null_;
length tmptxt $100.;
tmptxt="a\b Refer $crossref(Table 1) and $italic.Section $crossref(3.1)$enditalic";
tmptxt=prxchange('s/([\\{}])/\\$1/',-1,tmptxt);
tmptxt=prxchange('s#[\$]([a-z]+)(?=\()#%$1#i',-1,tmptxt);
tmptxt=prxchange('s#[\$]([a-z]+)#&$1#i',-1,tmptxt);
put tmptxt;
run;
Hi FriedEgg
Thanks a bunch.
I reduced my code
Could you explain two things
Thanks in advance.
Rajaram
This is a non-capturing group that performs a positive look-ahead assertion.
[\$]([a-z]+)(?=\() Options: case insensitive; ^ and $ match at line breaks Match a $ character «[\$]» Match the regular expression below and capture its match into backreference number 1 «([a-z]+)» Match a single character in the range between “a” and “z” «[a-z]+» Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=\()» Match the character “(” literally «\(»
I could help you to understand what the prxchange function is doing, as you are aware the prxchange function is used to replace the captured text and replace with another text or make it blank. Here are explanations of what the prxchange function is trying to do. prxchange is best for these kind of replacements.
prxchange('s/([\\{}])/\\$1/',-1,tmptxt);
/* trying to locate any one of \\{} and replace with
blank*/
prxchange('s/([\$][a-zA-Z]+)([\(])/%$1$2/',-1,tmptxt);
/* Trying to locate to capture buffers
first capture for $ followed by any alphabet followed by + and second capture bufferfor \(
and replace them with %*/
prxchange('s/%\$/%/',-1,tmptxt);
/*
prxchange('s/%\$/%/',-1,tmptxt);
/* trying to locate % followed by $ and when found this will be
replaced with %*/
prxchange('s/([\$][a-zA-Z]+)/&$1$2/',-1,tmptxt);
/* Trying to locate a text with $ followed by
any small or capital letter alphabet followed by + , then this whole text is when identified
will be replaced with &*/
/* trying to locate the special character like & $ and
prxchange('s/&\$/&/',-1,tmptxt);
replace with &*/
Thanks,
Jag
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.
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.