BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Feyng819
Obsidian | Level 7
How can I use prxchange to replace special characters like “/,&-“ with space?

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!
1 ACCEPTED SOLUTION

Accepted Solutions
tarheel13
Rhodochrosite | Level 12

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; 

View solution in original post

6 REPLIES 6
tarheel13
Rhodochrosite | Level 12

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; 
Feyng819
Obsidian | Level 7
Thanks for your help it works!
FreelanceReinh
Jade | Level 19

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.)

Feyng819
Obsidian | Level 7
Thanks for your suggestion this is also a way to go:)
Patrick
Opal | Level 21

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;

 

Feyng819
Obsidian | Level 7
Thanks for your suggestion:)

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 770 views
  • 10 likes
  • 4 in conversation