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

Good morning,

I would like to obtain the length of variables.  I use Length function and I found it works in text format, not in numeric format.  Any idea how to fix it?  Thanks.

 

data Test;  
	length ID $9 Zipcode 5; 
	infile datalines delimiter=','; 
	input ID Zipcode;  
	datalines;                     
	ER00002,87920,
	EC1R0192,6634,
	OK1R00013,28
;   

data test1;
	set test;
	ID_cat=length(ID);
	Zip_cat=length(Zipcode);
run;
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
	ID_cat=length(ID);
	Zip_cat=lengthn(put(Zipcode,5. -l));
data Test;  
	length ID $9 Zipcode 5; 
	infile datalines delimiter=','; 
	input ID Zipcode;  
	datalines;                     
	ER00002,87920,
	EC1R0192,6634,
	OK1R00013,28
;   

data test1;
	set test;
	ID_cat=length(ID);
	Zip_cat=lengthn(put(Zipcode,5. -l));
run;

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

If you want to know the defined length for a variable use VLENGTH() function.

 

The LENGTH of a variable in SAS is the number of bytes that SAS needs to store the data into the SAS dataset. For character variables it sets the maximum number of bytes that can be stored (note with mulit-byte encodings like UTF-8 the number of characters might be less than the number of bytes needed to represent those characters). For a NUMERIC variable the default length is 8 since SAS stores all numbers as 64-bit binary floating point numbers.  In general you should always just use 8 as the length to avoid loss of precision.  If you know your values will only ever be integers you could get away with storing less that the full 8 bytes into the dataset, but in general it is not worth the effort.

 

 

The LENGTH() function just returns the location of the last non-blank character in the string (by convention all all blank string will result in a length of 1).  If you call it with a numeric variable SAS will first convert the number into a character string using the BEST12. format so the result will always be 12 since the value is right aligned in the 12 character string that is generated.

novinosrin
Tourmaline | Level 20
	ID_cat=length(ID);
	Zip_cat=lengthn(put(Zipcode,5. -l));
data Test;  
	length ID $9 Zipcode 5; 
	infile datalines delimiter=','; 
	input ID Zipcode;  
	datalines;                     
	ER00002,87920,
	EC1R0192,6634,
	OK1R00013,28
;   

data test1;
	set test;
	ID_cat=length(ID);
	Zip_cat=lengthn(put(Zipcode,5. -l));
run;
PaigeMiller
Diamond | Level 26

Math will get you the "length" of integers.

 

zip_cat=floor(log10(zipcode))+1;

 

 

--
Paige Miller
Tom
Super User Tom
Super User

@PaigeMiller wrote:

Math will get you the "length" of integers.

 

zip_cat=floor(log10(zipcode))+1;

 

 


Or let the a FORMAT do the work of figuring out what width you would need on your format to display the values as an integer.

width = length(put(number,32.-L));

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2095 views
  • 3 likes
  • 4 in conversation