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

Hello, 

 

I am converting a character variable to a numeric variable using the INPUT function.  I created a new variable and specify a length using the informat argument, but the value defaults back to a length of 8 when I check the length with PROC CONTENTS.

 

Here is the code:

 

data example;

var1='165.45';

run;

 

data example2;

set example;

var2=input(var1, 10.);

run;

 

proc contents data=example2;

run;

 

Can someone explain why the length of var2 is 8 and not 10 as I specified in the input function?

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Numeric variables cannot have a length of 10.  SAS stores all numbers as 8 byte floating point numbers.

 

You are confusing the LENGTH (how many bytes it takes to store the variable in the dataset) with the display WIDTH (how many bytes it takes to display the formatted value as text or in your case how many characters to read to find the value).

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

From the docs:

 

SAS stores all numeric values in 8 bytes of storage unless you specify differently. This does not mean that a value is limited to 8 digits, but rather that 8 bytes are allocated for storing the value.

 

On any computer, there are limits to how large the absolute value of an integer can be. In SAS, this maximum integer value depends on two factors: · · the number of bytes that you explicitly specify for storing the variable (using the LENGTH statement) the operating environment on which SAS is running If you have not explicitly specified the number of storage bytes, then SAS uses the default length of 8 bytes, and the maximum integer then depends solely on what operating system you are using.

 

And a beautiful quote from @hashman  's paper: "Numeric variables all have the same memory length, 8, since in the physical memory each is nothing more but an 8-byte character string converted to the corresponding double-float number by the algorithm hidden behind the RB8 informat"

 

 

SASKiwi
PROC Star

All numeric variables are defined with 8 bytes of storage unless you use a LENGTH statement to shorten it. You are confusing the INFORMAT length of 10 which describes the number if digits read in not the storage size of the numeric variable.  

Tom
Super User Tom
Super User

Numeric variables cannot have a length of 10.  SAS stores all numbers as 8 byte floating point numbers.

 

You are confusing the LENGTH (how many bytes it takes to store the variable in the dataset) with the display WIDTH (how many bytes it takes to display the formatted value as text or in your case how many characters to read to find the value).

ballardw
Super User

No need to use two data steps to do that example.

Specifying a format on an input statement is much more complex than you may realize. Consider this code:

data example;

   var1='165.45';
   var2=input(var1, 10.);
   var3=input(var1, 2.);
   var4=input(var1, 2.1);

run;

Can you figure out why the values of var3 and var4 are what is shown.

And formats are sometimes much more important than the length of the variable.

proc print data=example noobs;
  format var1 $3. var2 best3. var3 z8.2;
run;

 

SAS Innovate 2025: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 4926 views
  • 4 likes
  • 5 in conversation