BookmarkSubscribeRSS Feed
thanikondharish
Fluorite | Level 6
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
Fluorite | Level 6
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
Fluorite | Level 6
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
Fluorite | Level 6
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".

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 2347 views
  • 2 likes
  • 5 in conversation