Hi,
The length field (LENGTH in the format catalog) is the actual maximal length in the format LABEL.
Question: is there any place where this information is used by the SAS system e.g. checking some information, used by an option...
for me.
What was the intention when it was decide to store the information in the format catalog?
I can find explaination with the min, max and default length but not with the actual maximal length.
I can only imagine extracting the format catalog in a dataset and use this information in a classical manner. But there a lengthn function would likely be sufficient
proc format;
value ny (default=7) 0='No'
1='Yes';
run;
proc format fmtlib;
run;
I don't really know what LENGTH is used for. MIN, MAX and DEFAULT are the key attributes. DEFAULT says what width to use if the format specification does not include a width. MIN and MAX sets the range of values you can use as the width.
Experiment and see.
proc format;
value ny
0='No'
1='Yes'
2='Padded '
;
run;
proc format cntlout=formats;
run;
proc print data=formats;
var fmtname start length min max default label;
run;
Basically the LENGTH seems to be set to the maximum length for any label value. If you don't overwide the DEFAULT length then that will be set to the same thing. It does not appear to make any attempt to figure out how to set MIN and MAX.
Generally what I run into with custom formats and default length if not supplied in the format definition is that the default display for "long" text gets truncated with the internal default length.
One place that the default may cause unexpected results is when concatenating formatted values when using the || operator or some of the string functions. You may find leading spaces in numeric values displaying formatted values because numeric results will default to right justification in most uses.
The Max comes into play when someone attempts, whether intentionally or not, to use a very large value for the Width of a format.
Suppose you accidentally type something like: Format somevar ny2000. ; That is not a syntax error and if your format does not have MAX value less than 2000 SAS will attempt to display Somevar using 2000 characters. Which would be overkill for "yes" or "no". So the default Max of 40 limits the result actually displayed.
@xxformat_com wrote:
But there a lengthn function would likely be sufficient
Not in cases where other formats are specified as labels (as in Example 4 of the VALUE statement documentation). Then the explicit length specifications of those formats (if any) are taken into account when the length of the new format is determined.
You're right I didn't think of that scenario. The lengthn function would not work there.
I just add an example for future reader of this post.
proc format;
value ny (default=7) 0='No'
1='Yes';
value nybis .=' '
other=[demo10.];
run;
proc format fmtlib;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.