Table of Contents
The LEFT function in SAS is a useful tool for extracting a specified number of characters from the beginning of a string. This function is particularly helpful for manipulating data in a dataset or creating new variables based on existing ones. To use the LEFT function, simply specify the string or variable from which you want to extract characters, along with the number of characters to extract. For example, the expression “LEFT(Name, 3)” would extract the first three characters from the variable “Name”. This function can also be nested within other functions to further manipulate the extracted characters.
Use the LEFT Function in SAS (With Example)
You can use the LEFT function in SAS to left-align character strings.
The LEFT function moves any leading blanks to the end of the string, which has the effect of left-aligning text without actually changing the length of the string.
The following example shows how to use the LEFT function in practice.
Example: How to Use LEFT Function in SAS
Suppose we have the following dataset in SAS that contains the names of various basketball teams:
/*create first dataset*/
data my_data;
input team $char20.;
datalines;
Mavericks
Kings
Hawks
Thunder
Rockets
Blazers
Nets
;
run;
/*view dataset*/
proc reportdata=my_data;
define team / display style=[asis=on];
run;

Note: We used PROC REPORT with the option DISPLAY STYLE=[ASIS=ON] to force SAS to display blanks in the output.
Notice that some of the team names have multiple leading blanks.
We can create a new dataset in which we use the LEFT function to move each of the leading blanks in each team name to the end of the string:
/*create new dataset*/
data new_data;
set my_data;
team_left = left(team);
run;
/*view new dataset*/
proc reportdata=new_data;
define team / display style=[asis=on];
run;

Notice that each of the names in the team_left column are now left aligned.
The LEFT function moved all leading blanks in each team name to the end of the string, which had the effect of left-aligning the strings.
Note that this is different than the TRIM function in SAS, which can be used to remove leading blanks entirely from strings.
The following tutorials explain how to perform other common tasks in SAS:
Cite this article
stats writer (2024). How can I use the LEFT function in SAS? Can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-left-function-in-sas-can-you-provide-an-example/
stats writer. "How can I use the LEFT function in SAS? Can you provide an example?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-left-function-in-sas-can-you-provide-an-example/.
stats writer. "How can I use the LEFT function in SAS? Can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-left-function-in-sas-can-you-provide-an-example/.
stats writer (2024) 'How can I use the LEFT function in SAS? Can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-left-function-in-sas-can-you-provide-an-example/.
[1] stats writer, "How can I use the LEFT function in SAS? Can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use the LEFT function in SAS? Can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
