SAS tips & tricks #8 – NOTE: Numeric values have been converted to charater values

In the last post SAS tips & tricks #7 we looked at how to avoid implicit character to numeric conversion.  Here we look at what causes numeric to character implicit conversions and how to avoid them

In a similar way to character to numeric implicit conversions, this  message occurs when you try to use a numeric variable in a function that requires a character argument, or to assign a numeric value to a variable which has already been defined as numeric.

It is possible to rely on SAS to perform these conversions imlpicitly, however, in general this is not considered good programming practice and it is usually preferable to perform these conversions explicitly. Implicit conversions also take more CPU time than explicit conversions and so may cause your program to execute more slowly.

Consider the following example, where we try to concatenate the character variable type with the numeric variable horsepower.

DATA cars;
  SET sashelp.cars;
  type_horse = STRIP(type)!!"-"!!horsepower;
RUN;

The output is created however the log displays the following message.

 43         DATA cars;
 44           SET sashelp.cars;
 45           type_horse = STRIP(type)!!"-"!!horsepower;
 46         RUN;
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
       45:34

This tells us that the code in line 45 of the log, at column 34 numeric variable has been used where a character variable was expected. In our case, the concatenation function (!!) expects a character variable, however horespower is numeric.

As the output dataset has been created, it may be tempting to leave the code as is, and have SAS perform the conversion for you. However, printing the dataset shows why this approach can be dangerous, as shown below.

Obs

 type_horse

1

SUV-         265

2

Sedan-         200

3

Sedan-         200

4

Sedan-         270

5

Sedan-         225

6

Sedan-         225

7

Sports-         290

Note how instead of being concatenated directly onto the hyphen, the horsepower, there are many leading spaces prior to the digits. These leading spaces are created by the implicit data conversion. When SAS performs this implicit numeric to character conversion it must select the format to use when performing the conversion and the width of the converted string. By default, SAS uses a format of BEST12, since our horsepower values each contains three digits, it means that SAS pads the values with nine leading spaces when performing the concatenation.

This issue can be avoided by first using the PUT function to convert the values from numeric to character. As shown in the example below.

DATA cars;
  SET sashelp.cars;
  type_horse = STRIP(type)!!"-"!!PUT(horsepower,3.);
RUN;

The second argument of the PUT functions allows you to specify the format that should be used during the conversion. As the horsepower values all have three digits, we specify a format of 3., which means that the value of horsepower is written as a character string of three digits in the concatenation. This also removes the converted to warning message from the log.

Finally a prinnt of the dataset confirms that the leading spaces have indeed been removed.

Obs

type_horse

1

SUV-265

2

Sedan-200

3

Sedan-200

4

Sedan-270

5

Sedan-225

6

Sedan-225

 

Leave your comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>