Aniruddh
Aniruddh

Reputation: 49

Long Numeric to Character in SAS

When I try to convert a 10 digit number which is stored as 8. into a character using put(,8.) it gives me the character value as something like 1.2345e10. I want the character string to be as 1234567890. Can someone please help ?

Upvotes: 4

Views: 2021

Answers (3)

Tom
Tom

Reputation: 51566

SAS stores numbers as IEEE 64bit floating point numbers. So when you see that the LENGTH of the variable is 8 that just means you are storing the full 8 bytes of the floating point representation. If the length is less than 8 then you will lose some of the ability to store more digits of precision, but when it pulls the data from the dataset to be used by a data step or proc it will always convert it back to the full 64 bit IEEE format.

The LENGTH that you use to store the data has little to do with how many characters you should use to display the value for humans to read. How to display the value is controlled by the FORMAT that you use. What format to use will depend on the type of values your variable will contain.

So if you know your values are always integers and the maximum value is 9,999,999,999 then you can use the format 10. (also known as F10.).

charvar= put(numvar,F10.);

Upvotes: 0

Allan Bowe
Allan Bowe

Reputation: 12691

Unless very sure of your inputs, I find it best to use best.:

data have;
  x=1234567890;output;
  x=12345.67890;output;
  x=.1234567890;output;
  x=12345678901234;output;
run;

data want;
  set have;
  length ten best $32;
  ten=put(x,10.);
  best=put(x,best32.);
run;

Note that using 10. here would wipe out any decimals, as well as larger numbers:

enter image description here

Upvotes: 2

Joe
Joe

Reputation: 63424

8. is the width (number of characters) you'd like to use. So of course it is 1.2345e9; that's what it can fit in 8 characters.

x_char = put(x,10.);

That asks for it to be put in 10 characters. Keep extending it if you want it more than 10. Just be aware you may need to use the optional alignment option (put(x,10. -l)) if you aren't happy with the default alignment of right aligned for shorter-than-maximum-length values.

Do note that when you originally describe the number as 8., I suspect you're actually referring to length; length is not the display size but the storage size (bytes in memory set aside). For character variables in a SBCS system they're identical, but for numeric variables length and format are entirely unrelated.

Upvotes: 4

Related Questions