SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Search||next

Transforming Data with SAS Functions
Converting Data with Functions

 
Automatic Character-to-Numeric Conversion

Let's begin with the automatic conversion of character values to numeric values.

By default, if you reference a character variable in a numeric context, such as an arithmetic operation, SAS software tries to convert the variable values to numeric. For example, in the DATA step below, the character variable PayRate appears in a numeric context. It is multiplied by the numeric variable Hours to create a new variable named Salary.

     data hrd.newtemp;
        set hrd.temp;
        Salary=payrate*hours;
     run;

When this step is executed, SAS software automatically converts the character values of PayRate to numeric values so the calculation can occur. This conversion is completed by creating a temporary numeric value for each character value of PayRate. This temporary value is used in the calculation. The character values of PayRate are not replaced by numeric values.

Whenever data is automatically converted, a message is written to the SAS log stating that the conversion has occurred.


SAS Log
4       data hrd.newtemp;
5       set hrd.temp;
6       Salary=payrate*hours;
7       run;

     NOTE: Character values have been converted
           to numeric values at the places given
           by: (Line):(Column).

           6:11
NOTE: The data set Hrd.Newtemp has 40 observations
      and 19 variables.
NOTE: The data statement used 0.78 seconds.


When Automatic Conversion Occurs

Automatic character-to-numeric conversion occurs when a character value is

  • assigned to a previously defined numeric variable, such as the numeric variable Rate
         Rate=payrate;
    

  • used in an arithmetic operation
         Salary=payrate*hours;
    

  • compared to a numeric value with a comparison operator
         if payrate>=rate;
    

  • specified in a function that requires numeric arguments.
         NewRate=sum(payrate,raise);
    

|next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement