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;

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.