BookmarkSubscribeRSS Feed
MarcTC
Obsidian | Level 7
I want to know how to replace special characters with a blank character.

In the following example, all special characters ; - / * ^ ) in String1 are replaced by a blank character and output as String2

String1 = YT;-/*ADD^)M
String2 = YT____ADD__M

I used _ in String2 to represent a blank character.
5 REPLIES 5
deleted_user
Not applicable
data x;
String1 = 'YT;-/*ADD^)M';
String2 = translate(String1,' ','`~!@#$%^&*()_+-=[]\{}|;:",./<>?');
put "String1=" String1 / "String2=" String2;
run;


If you have any more special characters to replace, just add a blank to the "to" list in the TRANSLATE function and your special character to the "from" string and all is well. I'm sure there are more elegant ways, but this works.

P.S. if you look at this with, say, SAS code font it shows that the "to" string actually DOES have 32 blanks to match the 32 special characters. Stupid forum. 😄

P.P.S. Not that it matters. You can put in one blank in the "to" string and be fine.
"Values of to and from correspond on a character-by-character basis; TRANSLATE changes the first character of from to the first character of to, and so on. If to has fewer characters than from, TRANSLATE changes the extra from characters to blanks. If to has more characters than from, TRANSLATE ignores the extra to characters."


Message was edited by: mpiehl Message was edited by: mpiehl
chang_y_chung_hotmail_com
Obsidian | Level 7
Sometimes it is easier to say "anything other than A and B" than to list each and every single thing that is not A or B.




/* convert any non-alphabet chars to a blank */


data _null_;


  v1 = "YT;-/*ADD^)M";


  v2 = prxchange("s/[^A-Z]/ /",-1,v1);


  put (_all_) (=/);


run

crowsea
Calcite | Level 5
I tried the example given. The v2 created is the same string as v1. There was no warning or error given.
Astounding
PROC Star
Presumably, you would like to have someone read your post and explain what happened. For that to happen, you need to post the log from your program.
Patrick
Opal | Level 21

Below two coding options for replacing any non alphanumeric character (or underscore) with a blank.

If below sample code doesn't work for your real data then please be very specific what's not working and ideally provide amended sample data that demonstrates where the current result doesn't meet your desired result.

data _null_;
  input have :$40.;

  /* create target variables with same type and length than source variable */
  if 0 then 
    do;
      want1=have;
      want2=have;
    end;

  /* option for single byte characters only */
  want1=prxchange('s/\W/ /',-1,strip(have));

  /* option that also allows for multi byte characters */
  want2=ktranslate(strip(have), ' ', kstrip(kcompress(have, ,'n')));

  /* print want variables */
  file print;
  put 
    'have: '  @10 have /
    'want1: ' @10 want1 /
    'want2: ' @10 want2 /
    '-------------------------'
    ;
datalines4;
YT;-/*ADD^M
Y9;-/*A;;D@D^M
;;;;
run; 

Patrick_0-1728003797728.png

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 24678 views
  • 2 likes
  • 6 in conversation