02-10-2017 10:15 AM
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.
02-10-2017 10:25 AM
proc sql noprint; select max(length(variable)) into :varlength from have; quit; data want; length variable $&varlength.; set have; run;
quick and crude, but should work.
02-10-2017 10:44 AM - edited 02-10-2017 01:29 PM
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 _temporary_; do _i_ = 1 to dim(_c); _s[_i_] = max(_s[_i_],length(_c[_i_])); end; if _eof then do _i_ = 1 to dim(_c); length _name_ $32; _name_ = vname(_c[_i_]); _length_=_s[_i_]; output; end; run; proc print; run; 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_,' ','='); put; end; set size; put 'Length ' _name_ '$' _length_ ';'; run; data resized; %include FT23F001 / source2; set &data; run; proc contents varnum; run; proc contents data=&data varnum; run;