Convert from Numeric to Character in SAS – The Easy and the Right Way
Last week in the blog post Convert From Character to numeric in SAS – The Easy and the Right Way, I showed how to convert from character to numeric in SAS. Also I showed examples of how not to do it. In this post, I show you how to convert from numeric to character. First I show you a way too common shortcut of doing this. Then I show you why not to take this shortcut. Finally i show you the correct way using the PUT Function.
Numeric to Character – The Easy Way
“I have a numeric variable, but i want it to be character. Luckily, I can use a concatenation function like CATS or CATX on it, and it becomes a character variable“. True. When you use a numeric variable in a character context, SAS will interpret it as a character value if it makes sense.
Let us give it a try. A Zip Code is a number that I will store as a character variable because it has no meaning in an arithmetic operation. A Zip Code usually has the same length for all possible values. In Denmark it always contains four digits. No exceptions.
data NumericToChar1; ZIP = 2100; /* Some number */ CharZIP = cats(ZIP); /* Naive conversion method */ put CharZIP=; /* Print to log */ run; ods select Variables; /* Select variable information */ proc contents data=NumericToChar1; run;
To the right, you can see the output from PROC CONTENTS. You can see that CharZIP is indeed a character variable. You can also see that it has length 200, even though it has only four digits. This is definitely not desirable. Why would you want to allocate a length of 200 to a variable, that only needs length 4? This happens because the default length of most character functions like CATS and CATX is 200.
The Right Way – PUT Function
As you can see in the above example, using a concatenation operator to convert a numeric variable to character is not an efficient method. This is due to the fact that you can not control the length of the converted string. You can achieve this control by means of the PUT Function. Let us see an example of doing the same conversion using the PUT Function.
data NumericToChar2; ZIP = 2100; /* Some number */ CharZIP = put(ZIP, 4.); /* Correct conversion method */ put CharZIP=; /* Print to log */ run; ods select Variables; /* Select variable information */ proc contents data=NumericToChar2; run;
To the right you can see the variable characteristics from PROC CONTENTS. Now, CharZIP has a length of 4, which is exactly what it needs. This is definitely preferred over a length of 200, which we can not even control.
In conclusion, you should not rely on character concatenation methods to convert from numeric to character in SAS. As you have seen, this method does not give you control over the length of the converted character variable or control over the conversion itself. That is why you should only use the PUT Function when doing this conversion. In my opinion, if you look at the conversion problem at bit more rigorously, it also shows a lack of understanding of what you want to achieve with the conversion when you use the ‘fast-track’ method in the first example.
You can download the entire program from this post here.