BookmarkSubscribeRSS Feed
thanikondharish
Calcite | Level 5
Data have;
String1='aaaaa*bbbb*ddddddd@jjjjjj';
Output;
String2='&aaaa*ggggghgg####';
Output;
Run;

How to replace special symbols by space
8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

When you say 'Special Symbols', do you simply mean *, @, &, # or?

thanikondharish
Calcite | Level 5
If more symbols how can do
PeterClemmensen
Tourmaline | Level 20

One way is to use PRXCHANGE like this. You can add more symbols of you like.

 

Data have;
String1='aaaaa*bbbb*ddddddd@jjjjjj';
String2='&aaaa*ggggghgg####';
Run;

data want;
 set have;
 String1 = prxchange('s/[*@&#]/ /', -1, String1);
 String2 = prxchange('s/[*@&#]/ /', -1, String2);
run;
gamotte
Rhodochrosite | Level 12

Converseley, you can indicate which characters you do not want to be replaced by spaces.

For instance, the following program wil replace all characters but alphanumerics and '_' with spaces :

 

data want;
 set have;
 String1 = prxchange('s/[^\w]/ /', -1, String1);
 String2 = prxchange('s/[^\w]/ /', -1, String2);
run;

 

thanikondharish
Calcite | Level 5
Don't mention symbols in coding
Tom
Super User Tom
Super User

You can use the TRANSLATE() function to replace characters.

Example:

data have;
  input string $40. ;
cards;
aaaaa*bbbb*ddddddd@jjjjjj
&aaaa*ggggghgg####
;

data want ;
  set have;
  new_string=translate(string,' ','*&#@');
run;

Results:

Obs             string              new_string

 1     aaaaa*bbbb*ddddddd@jjjjjj    aaaaa bbbb ddddddd jjjjjj
 2     &aaaa*ggggghgg####            aaaa ggggghgg

If you are not sure what to replace perhaps you use the COMPRESS() function to get the list of non-alpha characters in the string.

data want ;
  set have;
  new_string=translate(string,' ',compress(string,,'ad'));
run;
thanikondharish
Calcite | Level 5
Sorry already I have done with same code .
Is there any other way
ballardw
Super User

@thanikondharish wrote:
Data have;
String1='aaaaa*bbbb*ddddddd@jjjjjj';
Output;
String2='&aaaa*ggggghgg####';
Output;
Run;

How to replace special symbols by space

SHOW the required output or explicitly list which characters you consider "special characters". For some purpose I might consider "a" as a "special character".

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
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
  • 8 replies
  • 1341 views
  • 2 likes
  • 5 in conversation