BookmarkSubscribeRSS Feed
mcook
Quartz | Level 8

Is there a function to replace all characters that are not letters, numbers or underscores,  to an underscore?  

 

I am nearly certain i've run across one before, and used it.  But I have been unable to find it again.

I recall the function having at least 3 arguments, one of them being modifiers, allowing you to select all letters and numbers to the list of acceptable characters.

 

I thought it was TranWrd but that does not have a modifiers argument.  Same with Translate. 

 

any ideas?

4 REPLIES 4
ballardw
Super User

If you are getting such characters in variable names then it has something with how you are reading the data and I strongly suspect Proc Import.

 

Very simple to prevent: Options validvarname=V7;

prior to proc import.

That will force Proc import to create variable names compatible with the SAS version 7, i.e. no characters other than letters, digits and _.

Much easier to prevent than to correct later.

 

 

Reeza
Super User

NLITERAL function?

NVALID function may also be useful.

 

COMPRESS() is the only function I can think that comes close to what you've described but I agree with @ballardw that changing how you import your data is a better overall solution.

 

 

PGStats
Opal | Level 21

You can always do:

 

str_ = prxchange("s/[[:^alnum:]]/_/o", -1, strip(str));

 

 

PG
Ksharp
Super User
str_ = prxchange("s/\W/_/o", -1, strip(str));

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 1489 views
  • 5 likes
  • 5 in conversation