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 PROC CONTENTS 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
Note: The dollar sign “$” following a variable name tells SAS that the variable is a character variable.
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
How to Use Datalines Statement in SAS
How to Create New Variables in SAS
How to Remove Duplicates in SAS