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?
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.
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.
You can always do:
str_ = prxchange("s/[[:^alnum:]]/_/o", -1, strip(str));
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.