DATA Step, Macro, Functions and more

how do I check the length of a variable

Established User
Posts: 1

how do I check the length of a variable

I have a dataset with about 500,000 records in it and every one of my character variables has a length of 255. I would like to shorten them without truncating any values. I can make a guess, but I don't want to do that. I haven't been able to figure out a way to see what the longest value is. Let's say the longest piece of text only uses 25 of the 255 spaces available. How do I figure that out?


This is a static dataset so once I find what the true length of the longest value for a variable is, I do not need to worry about any future values going any longer than that.


Any help would be most appreciated.

Super User
Posts: 10,271

Re: how do I check the length of a variable

Posted in reply to zimmy_zim
proc sql noprint;
select max(length(variable)) into :varlength from have;

data want;
length variable $&varlength.;
set have;

quick and crude, but should work.

Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Respected Advisor
Posts: 3,852

Re: how do I check the length of a variable

[ Edited ]
Posted in reply to zimmy_zim

You want a method that looks at all the character variables without having to name them all and you want the keep the varnum order of the original data.  My example uses sashelp.heart which has only one character variable that needs to be re-sized.


You'll know it's working when you see warnings like this.

WARNING: Multiple lengths were specified for the variable DeathCause by input data set(s). This can cause truncation of data


%let data=sashelp.heart;
data size(keep=_name_ _length_);
   set &data end=_eof;
   array _c[*] _character_;
   array _s[10] _temporary_;
   do _i_ = 1 to dim(_c);
      _s[_i_] = max(_s[_i_],length(_c[_i_]));
   if _eof then do _i_ = 1 to dim(_c);
      length _name_ $32;
      _name_ = vname(_c[_i_]);
proc print;
filename FT23F001 temp;
options missing=' ';
data _null_;
   file FT23F001;
   if 0 then set &data;
   if _n_ eq 1 then do;
      put 'Retain  ' (_all_) (=) ';' @;
      _file_ = translate(_file_,' ','=');
   set size;
   put 'Length ' _name_ '$' _length_ ';';

data resized;
   %include FT23F001 / source2; 
   set &data;
proc contents varnum;
proc contents data=&data varnum;



Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation