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

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

1 ACCEPTED SOLUTION

Accepted Solutions
FriedEgg
SAS Employee

 

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;

 

View solution in original post

4 REPLIES 4
FriedEgg
SAS Employee

 

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;

 

Rajaram
Obsidian | Level 7

Hi FriedEgg

 

Thanks a bunch.

 

I reduced my code Smiley Happy

 

Could you explain two things

  1. What is the role of ( )? becuase i misplaced in my code
  2. I know ? is zero or one time. what is the meaning of ?=

 

Thanks in advance.

Rajaram

 

FriedEgg
SAS Employee

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 «\(»
Jagadishkatam
Amethyst | Level 16

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 &*/


prxchange
('s/&\$/&/',-1,tmptxt);
/* trying to locate the special character like & $ and
replace with &*/

 

Thanks,

Jag

 

Thanks,
Jag

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 4 replies
  • 1116 views
  • 0 likes
  • 3 in conversation