- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
For example I have a variable look like “102/103-Apple&Orange” and I want to make it looks “102 103 Apple Orange”, how can I use prxchange to achieve this?
I was trying something like prxchange(‘s/[/-&,]/ /‘,-1, var); but cannot get the variable I want. Thanks for your help in advance!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
for one thing if you want to put '/' in prxchange function, it has to be preceded by '\'. you can try this code.
data test;
text='102/103-Apple&Orange,';
text_new=prxchange('s/[,&\/-]/ /', -1, text);
proc print;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
for one thing if you want to put '/' in prxchange function, it has to be preceded by '\'. you can try this code.
data test;
text='102/103-Apple&Orange,';
text_new=prxchange('s/[,&\/-]/ /', -1, text);
proc print;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello @Feyng819,
Why do you want to use a heavy tool for a simple task? For replacements of one character by another the TRANSLATE function is likely more efficient (and easier to work with) than PRXCHANGE:
Example:
31 data _null_; 32 c='102/103-Apple&Orange'; 33 c=translate(c, ' ', '/-&'); 34 put c; 35 run; 102 103 Apple Orange
The first character in the third argument is replaced by the first character in the second argument (here: a space). In the absence of more characters in the second argument, the remaining characters of the third argument are replaced with spaces by default. (So, even a zero-length string could be used as the second argument because of this default behavior.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Also that I agree in principal with @FreelanceReinh one possible reason to use a RegEx here could be "laziness" in case you don't know all the characters in advance that you want to replace.
With RegEx metacharacters in expressions allow you to define whole sets.
Below code will replace any sequence of characters that aren't letters, digits or an underscore with a single blank.
data test;
text='102/103-Apple&+?Orange,';
/* ensure variable text_new has same length than text */
if 0 then text_new=text;
/* replace any sequence of characters that aren't alphanumeric or underscore with a blank */
text_new=prxchange('s/\W+/ /', -1, trim(text));
run;
proc print data=test;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content