Why should I use the LENGTH statement in SAS?

The LENGTH statement in SAS is used to define the length of variables as numeric, character, or a combination of both. This statement is helpful as it allows users to reserve the exact amount of memory needed for each variable. This helps to prevent any errors and reduces the amount of memory needed to store data. Additionally, the LENGTH statement is useful for defining any default lengths for variables, which can save time when creating or modifying datasets.


You can use the LENGTH statement in SAS to specify the maximum length for the values of a variable.

The following example shows how to use this statement in practice.

Example: Using the LENGTH Statement in SAS

Suppose we create the following dataset in SAS that contains information about various basketball teams:

/*create dataset*/
data my_data;
    input team $ conference $ points;
    datalines;
Mavericks Southwest 22
Pacers Central 19
Cavs Central 34
Lakers Pacific 20
Heat Southeast 39
Warriors Pacific 22
Grizzlies Southwest 25
Magic Southeaset 29
;
run;

/*view dataset*/
proc print data=my_data;

Notice that some values in both the team and conference columns are cut off.

This is because the default length for character variables in SAS is 8 and some of the values in the team and conference columns exceed this length.

Fortunately, we can use the LENGTH statement to specify the maximum length for both the team and conference columns:

/*create dataset*/
data my_data;
    length team $9 conference $9;
    input team $ conference $ points;
    datalines;
Mavericks Southwest 22
Pacers Central 19
Cavs Central 34
Lakers Pacific 20
Heat Southeast 39
Warriors Pacific 22
Grizzlies Southwest 25
Magic Southeaset 29
;
run;

/*view dataset*/
proc print data=my_data;

Notice that none of the values in the team or conference columns are cut off this time since we specified a max length of 9 for each of those columns.

We can also use the function to view the length of each variable in our dataset:

proc contents data=my_data;

From the output we can see the max length for each variable:

  • Max length of conference: 9
  • Max length of points: 8
  • Max length of team: 9

The following tutorials explain how to perform other common tasks in SAS:

x