Showing posts with label Length. Show all posts
Showing posts with label Length. Show all posts

Feb 12, 2012

Length and Precision of SAS Variables....

Did you know that ?
- The maximum number of variables in a single SAS data set under Windows is 32,767.
- An observation under Windows cannot be longer than 5MB(ie 5*1024*1024).
- Therefore, if you want your data set to contain 32,767 character variables, the longest each variable can be is approximately 160 bytes.
- However, a DATA step can reference more than 32,767 variables, if you write only 32,767 or fewer variables to the data set. For example, you could drop some variables with a DROP= data set option. The maximum number of variables a DATA step can reference under Windows is 2,147,483,647.
- The default length of numeric variables in SAS data sets is 8 bytes. (You can control the length of SAS numeric variables with the LENGTH statement in the DATA step.) In SAS under Windows, the Windows data type of numeric values that have a length of 8 is LONG REAL. The precision of floating-point values is always accurate to 15 digits. Depending upon the number, the precision may be 16 digits of accuracy.
Significant Digits and Largest Integer by Length for SAS Variables under Windows
Length in BytesLargest Integer Represented ExactlyExponential NotationSignificant Digits Retained
38,1922133
42,097,1522216
5536,870,9122298
6137,438,953,47223711
735,184,372,088,83224513
89,007,199,254,740,99225315
This maximum number varies for every Operating System…
72057594037927936 – os/390 IBM Mainframes
9007199254740991 – OpenVMS
9007199254740992 – Windows / Unix
For example, if you know that a numeric variable always has values between 0 and 100, you can use a length of 3 to store the number and thus save space in your data set.
Here is an example:
Trying out numbers greater than max number for len 8 in the above table to see the results….employed best32. format.
data numbers;
input x;
format x best32.;
cards;
9007199254740992
9007199254740999
90071992547409921
9007199254740992123
;
run;
Look at the output for obs 3 & 4…The values are getting rounded off to the next number with trailing zeroes…
sas lengthnum
Note: Dummy variables (those whose only purpose is to hold 0 or 1) can be stored in a variable whose length is 3 bytes.

Feb 3, 2010

SAS Functions - Length / Lengthn / lengthc / Lengthm...

One might expect the LENGTH function for a NULL character string would return 0, but that is not correct. The length returned by Length() of a space(“”) or missing char value is 1 and not 0.
If you try the Length function on numbers, then you will have 12 returnde by default. You even get 12 when the numeric variable has a missing value. The reason for this is that the best12. is used when converting the numbers to characters for use with the Length functions… The numbers are right justified and therefore show as being 12 char long….
LENGTH Function – Returns the length of a non-blank character string, excluding trailing blanks, and returns 1 for a blank character string
LENGTHN Function – Returns the length of a non-blank character string, excluding trailing blanks, and returns 0 for a blank character string
The LENGTHN function returns an integer that represents the position of the rightmost non-blank character in string. If the value of string is blank or missing, LENGTHN returns a value of 0. If string is a numeric variable (either initialized or uninitialized), LENGTHN returns a value of 12 and prints a note in the SAS log that the numeric values have been converted to character values.
LENGTHM Function – Returns the amount of memory (in bytes) that is allocated for a character string
The LENGTHM function returns an integer that represents the amount of memory in bytes that is allocated for string. If string is a numeric variable (either initialized or uninitialized), LENGTHM returns a value of 12 and prints a note in the SAS log that the numeric values have been converted to character values.
LENGTHC Function – Returns the length of a character string, including trailing blanks
The LENGTHC function returns an integer that represents the position of the rightmost blank or non-blank character in string. If the value of string is missing and contains blanks, LENGTHC returns the number of blanks in string. If the value of string is missing and contains no blanks, LENGTHC returns a value of 0. If string is a numeric variable (either initialized or uninitialized), LENGTHC returns a value of 12 and prints a note in the SAS log that the numeric values have been converted to character values.
Comparisons
-LENGTH returns a value of 1 for blank character strings, whereas LENGTHN returns a value of 0.
-The LENGTHN and LENGTH functions return the same value for non-blank character strings.
-LENGTH always returns a value that is less than or equal to the value returned by LENGTHC / LENGTHM.
-LENGTHN always returns a value that is less than or equal to the value returned by LENGTHC.
-LENGTHM always returns a value that is greater than or equal to the values returned by LENGTH, LENGTHC, and LENGTHN
-LENGTHC always returns a value that is greater than or equal to the values returned by LENGTH and LENGTHN.
-LENGTHC always returns a value that is less than or equal to the value returned by LENGTHM.
See the example below executed in SAS…
sas functions
Try this code….
data lenchar;
 
length example $ 25;
ex1=''; /*missing value*/
example="''";
len=length(ex1); /* 1 notice this */
lenm=lengthm(ex1); /* 1 */
lenn=lengthn(ex1); /* 0 notice this */
lenc=lengthc(ex1); /* 1 */
output;
 
ex2=' ';
example="' '";
len=length(ex2); /* 1 notice this */
lenm=lengthm(ex2); /* 1 */
lenn=lengthn(ex2); /* 0 notice this */
lenc=lengthc(ex2); /* 1 */
output;
 
ex3='ABCDEF';
example="'ABCDEF'";
len=length(ex3);
lenm=lengthm(ex3);
lenn=lengthn(ex3);
lenc=lengthc(ex3);
output;
 
 
ex4='sas';
example="'sas'";
len=length(ex4);
lenm=lengthm(ex4);
lenn=lengthn(ex4);
lenc=lengthc(ex4);
output;
 
ex5='ABCDEF  ';
example="'ABCDEF  '";
len=length(ex5); /* 6 notice this*/
lenm=lengthm(ex5); /* 8 notice this*/
lenn=lengthn(ex5); /* 6 notice this*/
lenc=lengthc(ex5); /* 8 notice this*/
output;
 
exmp1=123; /*numeric value*/
example="123";
len=length(exmp1); /* 12 notice this*/
lenm=lengthm(exmp1); /* 12 notice this*/
lenn=lengthn(exmp1); /* 12 notice this*/
lenc=lengthc(exmp1); /* 12 notice this*/
output;
 
exmp2=123.45;
example="123.45";
len=length(exmp2);
lenm=lengthm(exmp2);
lenn=lengthn(exmp2);
lenc=lengthc(exmp2);
output;
 
exmp3=12345678901234567890;
example="12345678901234567890";
len=length(exmp3);
lenm=lengthm(exmp3);
lenn=lengthn(exmp3);
lenc=lengthc(exmp3);
output;
 
exmp4='01Jan1960'd;
example="'01Jan1960'd";
len=length(exmp4);
lenm=lengthm(exmp4);
lenn=lengthn(exmp4);
lenc=lengthc(exmp4);
output;
 
drop ex1 ex2 ex3 ex4 ex5 exmp1 exmp2 exmp3 exmp4;
label len='length'
lenm='lengthm'
lenn='lengthn'
lenc='lengthc';
run;