BookmarkSubscribeRSS Feed
mrdlau
Obsidian | Level 7
I have a variable z=10, but in a character format.
To multiply it by 2, i convert the z to a numeric during my multiplication:
2*input(z, 8.2).

However, my result is .2, not 20. Why is this the case? It is onlu 20 when I use input(z, 8.)



4 REPLIES 4
Astounding
PROC Star

You are using the wrong informat:

 

input(z, 8.2)

 

You should be using:

 

input(z, 8.)

 

If you use 8.2, it is telling SAS to look for a decimal point.  If it doesn't find one, assume the last two digits belong after the decimal point. 

mrdlau
Obsidian | Level 7
Thank you! What is my z variable is really a variable? I,e. It could be greater or less than 1?
Astounding
PROC Star

Z is already a variable.  So I'm not sure what question you are asking.  At any rate .....

 

The meaning of 8.2 doesn't change.  It still means to read 8  characters, and  see if they contain a decimal point.  If there is a decimal point, use it.  But if there is no decimal point, assume the last two digits should fall after the decimal point.

 

If your original variable is numeric, rather than character, you are asking for trouble using the INPUT function.  The INPUT function expects to read a character string.

ballardw
Super User

@mrdlau wrote:
I have a variable z=10, but in a character format.
To multiply it by 2, i convert the z to a numeric during my multiplication:
2*input(z, 8.2).

However, my result is .2, not 20. Why is this the case? It is onlu 20 when I use input(z, 8.)




You might try a simple

  x = 2*z;

and examine the result and the log.

SAS will attempt to convert character  values to numeric to do computations like this in a data step. The conversion is usually pretty good. Though if you want to do calculations the question is always why was the variable created as character in the first place.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 660 views
  • 0 likes
  • 3 in conversation