DATA Step, Macro, Functions and more

Formatting question

Reply
Contributor
Posts: 43

Formatting question

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.)



Super User
Posts: 6,931

Re: Formatting question

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. 

Contributor
Posts: 43

Re: Formatting question

Thank you! What is my z variable is really a variable? I,e. It could be greater or less than 1?
Super User
Posts: 6,931

Re: Formatting question

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.

Super User
Posts: 13,926

Re: Formatting question


@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.

Ask a Question
Discussion stats
  • 4 replies
  • 117 views
  • 0 likes
  • 3 in conversation