of the Month
Transferring a numeric variable to a character value is a common task, a lot more common than people realize infact most people do not realize they are doing it. Take for example the following code that is inside a datastep:
if hours>40 then flagtxt='NOTE: Hours reported are greater than 40 reported '|| 'hours are: '||compress(put(hours,8.2.));
In the example we are placing the HOURS value that is stored as numeric to a character string. If we had just done:
if hours>40 then flagtxt='NOTE: Hours reported are greater than 40 reported '|| 'hours are: ||hours;
SAS would have brought up a message in the SAS LOG indicating that a numeric to character conversion had been done within SAS these messages should be looked at.
In the above example, the conversion is easy to spot, but not something like:
Tothours = tothours + hours;
where the variable HOURS is numeric and TOTHOURS is character here some very funny things can go on and unexpected results occur.
Let me show you now a classic example that I show people regarding this issue:
191 data _null_; 192 length x $8; 193 x = 1/3; 194 x = x * 3; 195 put x=; 196 run; NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column). 193:9 194:10 NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column). 194:8 x=0.999999
The answer for x should of course be 1, not 0.999999 what the program first did was calculate 1/3 and stored it as a character, then multiplied that value by 3. I did not copy across the LOG messages but it said that NOTE: Numeric values have been converted to character values at the places given by
To avoid the issues of numeric to character conversion, please always use a PUT statement with a format to put the correct numeric value into a character variable.
Next month, Character to Numeric conversion.
Updated August 1, 2008