• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / SAS / SAS left() Function – Left Align Character Variables in Data Step

SAS left() Function – Left Align Character Variables in Data Step

April 12, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Identifying Duplicates in SAS with PROC SORT dupout Option
  • 2.  SAS substr() Function – Get Substring from Character Variable
  • 3.  SAS Comments – Commenting Your SAS Code the Right Way
  • 4.  SAS rename Statement – How to Rename Variables in a Data Step
  • 5.  SAS Dollar Format – Formatting Numbers as Dollars in SAS Dataset
  • 6.  Set Multiple Datasets in SAS Data Step
  • 7.  How to Combine Datasets Vertically in SAS
  • 8.  SAS ceil – Round Up to Ceiling of Number in a SAS Data Step
  • 9.  SAS case when – Conditional Logic with case Expression in PROC SQL
  • 10.  SAS mean() Function – Find Average Across Columns in Data Step

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy