BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
riyaaora275
Obsidian | Level 7

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
CalleJ
Fluorite | Level 6

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.

View solution in original post

4 REPLIES 4
CalleJ
Fluorite | Level 6

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
SuryaKiran
Meteorite | Level 14
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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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