• 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 strip – Remove All Leading and Trailing Blanks from String Variable

SAS strip – Remove All Leading and Trailing Blanks from String Variable

January 24, 2022 Leave a Comment

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

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

/* Output: */
b=*this is a string with some leading and 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 strip() function removes leading trailing blank spaces from our string variables.

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

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

/* Output: */
b=*this is a string with some leading and 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.

In addition to the SAS strip() function, two other functions are the SAS compress() and SAS trim() functions.

The SAS compress() function gives us the ability to remove all blank spaces from a string, and the SAS trim() function removes 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 strip() function to remove all leading and trailing blanks from a string variable in a SAS data step.

Other Articles You'll Also Like:

  • 1.  Date Format ddmmmyyyy in SAS
  • 2.  mod Function in SAS – Find Remainder of 2 Numbers After Division
  • 3.  SAS prxmatch – Find Pattern in String Using Regex (Regular Expression)
  • 4.  SAS if then else – Write Conditional Expressions for Multiple Conditions
  • 5.  SAS rename Statement – How to Rename Variables in a Data Step
  • 6.  SAS trim – Remove All Trailing Blanks from String Variable in Data Step
  • 7.  yyyy-mm-dd Date Format in SAS
  • 8.  SAS Remove Labels from Dataset with PROC DATASETS
  • 9.  intcx SAS – Find Time Periods Between Two Dates in SAS Data Step
  • 10.  SAS Not Equal – Check if a Variable is Not Equal to Another 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