Google SAS Search

Add to Google

Monday, February 07, 2005

Valid Complaints?

Say you are programming away in SAS and you need to switch every occurence of a word with another word. You look at the SAS functions and happily see TRANWRD().

From the documentation: TRANWRD(source, target, replacement).

No problem; you stick it in your data step and it's on to the next thing. And so next you find that you need to get rid of some bad characters. You already know you can compress() the little buggers out (or maybe you didn't know? Compress() can be used to compress any character- not just spaces! There's an optional third argument where you can specify which character(s) you want to remove.) But that presents a little problem since it may cause some wordsToRunTogether. And you don't want that.

So you look in your documentation and find the TRANSLATE() function replaces specific characters in a character expression. Woohoo we are saved! We can replace bad characters with spaces. But check this out: TRANSLATE(source,to-1,from-1<,,from-n>)

Translate to from.
Tranwrd from to.

It leaves me scratching my head. Why?

And since I brought up the trandwrd() function and complaints, I really should point out that tranwrd() DOES NOT replace WORDS. But rather a pattern of characters. What!? If you want to translate words with the tranwrd() function you have to provide your own spaces.
* get rid of preposition FOR;
myText = tranwrd(myText, 'FOR', '''); * oops, FOREST turns into EST;

myText = tranwrd(myText, ' FOR ', '''); * now translating the word FOR;


  1. Nice blog.

    The translate tranwrd thing has been there a long time. After going through every line of SAS including all procs, we can say for sure that there are a LOT of weirdness places like this one.

  2. Oh yeah, here's a trivia question for you.

    There is only 1 option on a proc in all of SAS that takes a yes/no parameter rather than being a boolean flag. For example, instead of proc xyz option1 it requires proc xyz option1=yes.

    Name that proc and option...

  3. Actually, I was wrong. There are a couple but very, very few.

  4. Um, I have to admit I have no idea. Dammit! And I thought this blog was going to make me look smart! :)