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 report data=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 report data=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.
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
SAS: How to Remove Commas from String
SAS: How to Replace Characters in a String
SAS: How to Remove Special Characters from Strings