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.
2 REPLIES 2
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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 24025 views
  • 2 likes
  • 3 in conversation