The SAS left() function gives us the ability to left align character variables.

data k;
    a = '       this is a string with some leading blanks';
    b = left(a);
    put b=;
run;

/* Output: */
a=       this is a string with some leading blanks
b=this is a string with some leading blanks

When working with strings and character variables in SAS, the ability to manipulate and change their values easily can be very valuable.

One such change we can do is left align a character variable. The SAS left() function left aligns character variables.

By default, character variables are left aligned, but if you have leading blanks, you can use left() to move those blanks to the end of the value.

The difference between this and using strip() is that the variable length will stay the same when using left().

Below is an example of how to use the SAS left() function in SAS.

data k;
    a = '       this is a string with some leading blanks';
    b = left(a);
    put b=;
run;

/* Output: */
a=       this is a string with some leading blanks
b=this is a string with some leading blanks       

Using the SAS right() Function to Right Align Character Variables

If you want to right align a character variable, we can use the SAS right() function. The right() function does the opposite of what left() does.

If you have trailing blanks in your character variables, right() will right align your character variable and move the trailing blanks to the beginning of the value.

Below is an example of how to use the SAS right() function in SAS.

data k;
    a = 'this is a string with some trailing blanks       ';
    b = right(a);
    put b=;
run;

/* Output: */
a=this is a string with some trailing blanks
b=       this is a string with some trailing blanks

Hopefully this article has been useful for you to learn how to use the SAS left() function.

Categorized in:

SAS,

Last Update: February 26, 2024