DATA Step, Macro, Functions and more

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

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

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?


Accepted Solutions
Solution
a week ago
New Contributor
Posts: 3

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

Posted in reply to riyaaora275

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

Check your log for this:

 

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

 

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)Smiley SadColumn).
      1 at 26:5 

 

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

View solution in original post


All Replies
Super User
Posts: 8,216

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

Posted in reply to riyaaora275

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

 

Solution
a week ago
New Contributor
Posts: 3

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

Posted in reply to riyaaora275

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

Check your log for this:

 

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

 

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)Smiley SadColumn).
      1 at 26:5 

 

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

Super User
Super User
Posts: 9,840

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

Posted in reply to riyaaora275

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;
PROC Star
Posts: 631

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

Posted in reply to riyaaora275
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
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 63 views
  • 3 likes
  • 5 in conversation