Dan
Dan

Reputation: 10171

Sas9 Replace special characters with underscores

In Sas9, how can I replace all the , \ / or spaces, and other special characters of my choosing with underscores? A solution either in a datastep or in macro functions would do the trick, I'm just looking for a method to do it.

Thanks

Upvotes: 3

Views: 5778

Answers (2)

Dan
Dan

Reputation: 10171

The translate function might be what you're looking for

field2 = translate(trim(field_name),'_______',' ,.\/()')

Make sure to have as many underscores as you have special characters. Also, because you're translating spaces, you have to use the trim function or else you'll get a bunch of underscores after the name.

Upvotes: 2

itzy
itzy

Reputation: 11755

You can use the Perl regular expression functionality built into SAS.

data tmp;
 set tmp;
 var1 = prxchange('s/[,\/\\]/_/', -1, var);
run;

or something similar.

Upvotes: 10

Related Questions