LENGTH Statement

Specifies the number of bytes for storing variables.
Valid in: DATA step
Category: Information
Type: Declarative
See: LENGTH Statement under Windows, UNIX, and z/OS
CAUTION:
Avoid shortening numeric variables that contain fractions.
The precision of a numeric variable is closely tied to its length, especially when the variable contains fractional values. You can safely shorten variables that contain integers according to the rules that are given in the SAS documentation for your operating environment, but shortening variables that contain fractions might eliminate important precision.

Syntax

Arguments

variable-specification
is a required argument and has the form
variable(s)<$>length
variable
specifies one or more variables that are to be assigned a length. This includes any variables in the DATA step, including those dropped from the output data set.
Restriction: Array references are not allowed.
Tip:If the variable is character, the length applies to the program data vector and the output data set. If the variable is numeric, the length applies only to the output data set.
$
specifies that the preceding variables are character variables.
Default:SAS assumes that the variables are numeric.
length
specifies a numeric constant that is the number of bytes used for storing variable values.
Range:For numeric variables, 2 to 8 or 3 to 8, depending on your operating environment. For character variables, 1 to 32,767 under all operating environments.
DEFAULT=n
changes the default number of bytes that SAS uses to store the values of any newly created numeric variables.
Default:8
Range:2 to 8 or 3 to 8, depending on your operating environment.

Details

In general, the length of a variable depends on the following:
  • whether the variable is numeric or character
  • how the variable was created
  • whether a LENGTH or ATTRIB statement is present.
Subject to the rules for assigning lengths, lengths that are assigned with the LENGTH statement can be changed in the ATTRIB statement and vice versa. See SAS Variables in SAS Language Reference: Concepts for information about assigning lengths to variables.
Operating Environment Information: Valid variable lengths depend on your operating environment. For details, see the SAS documentation for your operating environment.

Comparisons

The ATTRIB statement can assign the length as well as other attributes of variables.

Example

This example uses a LENGTH statement to set the length of the character variable NAME to 25 bytes. The LENGTH statement also changes the default number of bytes that SAS uses to store the values of newly created numeric variables from 8 to 4 bytes. The TRIM function removes trailing blanks from LASTNAME before it is concatenated with these items:
  • a comma (,)
  • a blank space
  • the value of FIRSTNAME
If you omit the LENGTH statement, SAS sets the length of NAME to 32 bytes.
data testlength;
   informat FirstName LastName $15. n1 6.2;
   input firstname lastname n1 n2;
   length name $25 default=4;
   name=trim(lastname)||', '||firstname;
   datalines;
Alexander Robinson 35 11
;
proc contents data=testlength;
run;
proc print data=testlength;
run;
The following output shows a partial listing from PROC CONTENTS, as well as the report that PROC PRINT generates.
Partial PROC CONTENTS for TESTLENGTH
Partial PROC CONTENTS for TESTLENGTH
Setting the Length of a Variable
Setting the Length of a Variable

See Also

For information about the use of the LENGTH statement in PROC steps, see the Base SAS Procedures Guide.
Statements: