Previous Page | Next Page

Statements

LENGTH Statement



Specifies the number of bytes for storing variables.
Valid: in a DATA step
Category: Information
Type: Declarative
See: LENGTH Statement under Windows UNIX OpenVMS z/OS

Syntax
Arguments
Details
Comparisons
Examples
See Also

Syntax

LENGTH variable-specification(s)<DEFAULT=n>;


Arguments

variable-specification

is a required argument and has the form

variable(s)<$>length

where

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 32767 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.
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.   [cautionend]


Details

In general, the length of a variable depends on

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.  [cautionend]


Comparisons

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


Examples

This example uses a LENGTH statement to set the length of the character variable NAME to 25. It also changes the default number of bytes that SAS uses to store the values of newly created numeric variables from 8 to 4. The TRIM function removes trailing blanks from LASTNAME before it is concatenated with a comma (,) , a blank space, and the value of FIRSTNAME. If you omit the LENGTH statement, SAS sets the length of NAME to 32.

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.

Setting the Length of a Variable

                         The SAS System                        3

                       CONTENTS PROCEDURE

      -----Alphabetic List of Variables and Attributes-----
 
        #    Variable     Type    Len    Pos    Informat
        ------------------------------------------------
        1    FirstName    Char     15      8    $15.    
        2    LastName     Char     15     23    $15.    
        3    n1           Num       4      0    6.2     
        4    n2           Num       4      4            
        5    name         Char     25     38            
                         The SAS System                        4

OBS   FirstName   LastName      n1     n2          name

 1    Alexander   Robinson   0.35000   11   Robinson, Alexander

See Also

Statement:

ATTRIB Statement

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

Previous Page | Next Page | Top of Page