Monday, November 14, 2016

Hack 4.1 Avoiding the Hidden “Problem” in the TRANWRD Function

SAS Programming Professionals,

Did you know that:  the TRANWRD function, which can replace or remove all occurrences of a given word, has a hidden gotcha

The gotcha is that, if you do not provide a length statement for a new variable that receives the output from the TRANWRD function, that variable defaults to a length of 200 characters.

Here is an example of the gotcha:

data _null_;

length newstatement1 $34;

statement = "I enjoy going to SUGI conferences.";

newstatement1 = tranwrd(statement,"SUGI", "SGF");

newstatement2 = tranwrd(statement,"SUGI", "SGF");

length_newstatement1 = lengthc(newstatement1);

length_newstatement2 = lengthc(newstatement2);

put "length_newstatement1 = " length_newstatement1;
put "length_newstatement2 = " length_newstatement2;

run;

If you submit the code, above, you will see that newstatement1 ends up with a length of 34, while newstatement2 ends up with a length of 200.  That is because newstatement1 had a length statement specifying its size before it was used with the TRANWRD function.  Conversely, newstatement2 was created fresh and new when used in the TRANWRD function and SAS--not knowing what to make of this infant variable--decided to make it 200 characters in length; the default for the TRANWRD function.

So, what are the ramifications of this gotcha?  The ramifications are that you could end up with larger variables than you intend to--needlessly inflating the size of a SAS data set--if you are not careful to declare the length of new SAS character variables created by using the TRANWRD function.  You have been warned!

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers
http://www.amazon.com/Michael-A.-Raithel/e/B001K8GG90/ref=ntt_dp_epwbk_0

No comments:

Post a Comment