Obsidian | Level 7

## Why do numerical values with a character format work as numbers when applying mathematical functions

If i have a code like this

``````Data sample;
x='100';
y=10*x;
run;``````

the result would be y= 1000

But x is a character and not a number so applying a mathematical function to it should not work.

why does this work?

1 ACCEPTED SOLUTION

Accepted Solutions
Fluorite | Level 6

## Re: Why do numerical values with a character format work as numbers when applying mathematical funct

SAS tries to interpret the characters as numeric if forced to do so.

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

You will get another log message if the numeric conversion is unsuccessful.

If you run this code, SAS will try to convert the character "U" to numeric and fail.

``````Data sample;
x='U';
y=10*x;
run;``````

NOTE: Invalid numeric data, x='U' , at line 26 column 6.
x=U y=. _ERROR_=1 _N_=1
NOTE: Missing values were generated as a result of performing an operation on missing values.
Each place is given by: (Number of times) at (Line):(Column).
1 at 26:5

A healthy approach to SAS programming is to avoid these forced conversions.

4 REPLIES 4
Opal | Level 21

## Re: Why do numerical values with a character format work as numbers when applying mathematical funct

SAS was designed to do that. See: http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a001276672.htm

Art, CEO, AnalystFinder.com

Fluorite | Level 6

## Re: Why do numerical values with a character format work as numbers when applying mathematical funct

SAS tries to interpret the characters as numeric if forced to do so.

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

You will get another log message if the numeric conversion is unsuccessful.

If you run this code, SAS will try to convert the character "U" to numeric and fail.

``````Data sample;
x='U';
y=10*x;
run;``````

NOTE: Invalid numeric data, x='U' , at line 26 column 6.
x=U y=. _ERROR_=1 _N_=1
NOTE: Missing values were generated as a result of performing an operation on missing values.
Each place is given by: (Number of times) at (Line):(Column).
1 at 26:5

A healthy approach to SAS programming is to avoid these forced conversions.

Diamond | Level 26

## Re: Why do numerical values with a character format work as numbers when applying mathematical funct

This uses implicit conversion, i.e. if it can, SAS will automatically try to convert the text to number.  It is however IMO not recommended.  Never let the computer decide for you what you want to do.  Use explicit conversion always.

``````data sample;
x='100';
y=10*input(x,8.);
run;``````
Meteorite | Level 14

## Re: Why do numerical values with a character format work as numbers when applying mathematical funct

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

Did you notice this in log, SAS will automatically try to convert it into numeric.

If SAS can't convert the value then y will be missing. Check the log.

``````Data sample;
x='1,000';
y=10*x;
run;
``````
```NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
28:6
NOTE: Compression was disabled for data set WORK.SAMPLE because compression overhead would increase the size of the data set.
NOTE: Invalid numeric data, x='1,000' , at line 28 column 6.
x=1,000 y=. _ERROR_=1 _N_=1
NOTE: Missing values were generated as a result of performing an operation on missing values.
Each place is given by: (Number of times) at (Line):(Column).
1 at 28:5 ```
Thanks,
Suryakiran
Discussion stats
• 4 replies
• 868 views
• 3 likes
• 5 in conversation