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!
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).
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"
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.
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).
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;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.