How can the length of character variables be changed using the SAS language? 2

How can the length of character variables be changed using the SAS language?

The SAS language allows users to change the length of character variables through the use of the LENGTH statement. This statement allows for the modification of the length of a character variable to accommodate different data values. This feature is useful for manipulating and organizing data, as it allows for the creation of variables with specific lengths to suit the needs of the data analysis. The length of a character variable can be adjusted before or after the variable is created, providing flexibility in data management. With the ability to modify the length of character variables, SAS users have greater control over their data and can ensure accurate and efficient data analysis.

SAS: Change Length of Character Variables


The easiest way to change the length of character variables in SAS is to use the ALTER TABLE and MODIFY statements within PROC SQL.

You can use the following basic syntax to do so:

proc sql;
    alter table my_data
    modify team char(4);
quit;

This particular example modifies the length of the character variable called team in the dataset called my_data to have a length of 4.

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

Example: Change Length of Character Variable in SAS

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

/*create dataset*/
data my_data;
    input team $ points;
    datalines;
Cavs 12
Cavs 24
Heat 15
Cavs 26
Heat 14
Mavs 36
Mavs 19
Nets 20
Nets 31
;
run;

/*view dataset*/
proc printdata=my_data;

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

/*view length of each variable in dataset*/
proc contentsdata=my_data;

The last table in the output shows the length of each variable:

From this table we can see:

  • The points variable is a numeric variable with a length of 8.
  • The team variable is a character variable with a length of 8.

Suppose we would like to change the team variable to have a length of 4.

We can use the following syntax to do so:

/*change length of team variable to 4*/
proc sql;
    alter table my_data
    modify team char(4);
quit;

We can use them use PROC CONTENTS once again to view the length of each variable in the dataset:

/*view updated length of each variable in dataset*/
proc contentsdata=my_data;

The last table in the output shows the length of each variable:

We can see that the team variable now has a length of 4.

It’s worth noting that none of the values in the team column were truncated because the longest team name had a length of 4.

However, if you changed the length of the team variable to 3, for example, then the last character of some team names would have been truncated.

Using this method, no warning message will appear if truncation occurs so make sure you know the maximum length of strings in your character column before you use this method.

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

Cite this article

stats writer (2024). How can the length of character variables be changed using the SAS language?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-length-of-character-variables-be-changed-using-the-sas-language/

stats writer. "How can the length of character variables be changed using the SAS language?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-length-of-character-variables-be-changed-using-the-sas-language/.

stats writer. "How can the length of character variables be changed using the SAS language?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-length-of-character-variables-be-changed-using-the-sas-language/.

stats writer (2024) 'How can the length of character variables be changed using the SAS language?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-length-of-character-variables-be-changed-using-the-sas-language/.

[1] stats writer, "How can the length of character variables be changed using the SAS language?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can the length of character variables be changed using the SAS language?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top