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?
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):(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.
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
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):(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.
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;
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.