Table of Contents
The LENGTH statement in SAS is a useful tool for defining the length of character variables in a data set. It allows users to specify the maximum number of characters that can be stored in a variable, ensuring that the data is accurately represented and preventing truncation. This statement can be utilized in data step programming to create new variables or modify existing ones, and can also be used in PROC SQL to define column lengths in tables. Additionally, the LENGTH statement can be used in conjunction with other SAS functions and statements to manipulate and analyze data effectively. Overall, the LENGTH statement is a crucial component of SAS programming and plays a vital role in data management and analysis.
Use the LENGTH Statement in SAS
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 printdata=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 printdata=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 contentsdata=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:
Cite this article
stats writer (2024). How can the LENGTH statement be utilized in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-length-statement-be-utilized-in-sas/
stats writer. "How can the LENGTH statement be utilized in SAS?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-length-statement-be-utilized-in-sas/.
stats writer. "How can the LENGTH statement be utilized in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-length-statement-be-utilized-in-sas/.
stats writer (2024) 'How can the LENGTH statement be utilized in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-length-statement-be-utilized-in-sas/.
[1] stats writer, "How can the LENGTH statement be utilized in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can the LENGTH statement be utilized in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
