• 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 trim – Remove All Trailing Blanks from String Variable in Data Step

SAS trim – Remove All Trailing Blanks from String Variable in Data Step

January 24, 2022 Leave a Comment

To remove all trailing blanks from a string variable in a SAS data step, we can use the SAS trim() function.

data k;
    a = 'this is a string with some trailing blanks            ';
    b = trim(a) || "*";
    put b=;
run;

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

When working with datasets which contain string and character variables, removing unwanted blank spaces can save space as well as ensure the data is displayed as desired in our reports.

The SAS trim() function removes trailing blank spaces from our string variables.

You can see how to use the SAS trim() function in a data step to remove blank spaces after the last character in the following SAS code.

data k;
    a = 'this is a string with some trailing blanks            ';
    b = trim(a) || "*";
    put b=;
run;

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

The Difference between trim(), compress(), and strip() in SAS

When working with string variables in SAS, there are a few useful functions for cleaning up whitespace and removing blanks.

Two such functions are the SAS compress() and SAS strip() functions.

The SAS compress() function gives us the ability to remove all blank spaces from a string, and the SAS strip() function removes both leading and trailing blank spaces from a string.

You can see below how each of these string manipulation functions work in the following SAS code:

data k;
    a = '     abc de fghi jkl         mnop     ';
    trim =  "*" || trim(a) || "*";
    comp =  "*" || compress(a) || "*";
    strip =  "*" || strip(a) || "*";
    put trim=;
    put comp=;
    put strip=;
run;

/* Output: */
trim=*     abc de fghi jkl         mnop*
comp=*abcdefghijklmnop*
strip=*abc de fghi jkl         mnop*

Hopefully this article has been beneficial for you to understand how to use the SAS trim() function to remove all trailing blanks from a string variable in a SAS data step.

Other Articles You'll Also Like:

  • 1.  Get the Row Number in a SAS Data Step with _n_
  • 2.  SAS prxmatch – Find Pattern in String Using Regex (Regular Expression)
  • 3.  SAS compress – Remove Whitespace and Characters from String
  • 4.  SAS Less Than or Equal to with LE or <=
  • 5.  Set Multiple Datasets in SAS Data Step
  • 6.  SAS max() Function – Find Maximum Value Across Columns in Data Step
  • 7.  SAS Remove Formats from Dataset with PROC DATASETS
  • 8.  Get Last Observation of SAS Dataset with end=
  • 9.  SAS rename Statement – How to Rename Variables in a Data Step
  • 10.  Get Substring from Right in SAS

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