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));

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 723 views
  • 5 likes
  • 5 in conversation