• 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
  • VBA
  • About
You are here: Home / SAS / Get Substring from Right in SAS

Get Substring from Right in SAS

April 11, 2022 Leave a Comment

To get a substring from the right in a SAS data step, you can use the SAS substr() function and specify a start position with help from the length function.

data k;
        var = "string";
        last_two_chars = substr(var, length(var) - 2,2); 
        all_but_last_two = substr(var, 1, length(var) - 2); 
        put substr_from_right=;
run;

/* Output */
last_two_chars=ng
all_but_last_two=stri

When working with character variables in SAS, the ability to manipulate and get information from these variables is valuable.

One such piece of information are substrings of a variable.

The SAS substr() function allows us to easily get substrings from our variables in data steps.

substr() takes in 3 arguments. The first argument is the string you want to extract a substring from. The second argument is the starting position of your substring. For the third argument, you provide the number of characters you want to read.

If you want to get a substring of a string and start from the right of the string, we can use substr() and use length() to pass different second and third arguments.

For example, if you want to get all characters except the last two characters of a string, you can use the length() function and subtract two from it as shown below.

data k;
        var = "string";
        all_but_last_two = substr(var, 1, length(var) - 2); 
        put substr_from_right=;
run;

/* Output */
all_but_last_two=stri

If you instead want to get just the last two characters from a string, you can start your substring at the length of the variable minus two position and go for two characters as shown below.

data k;
        var = "string";
        last_two_chars = substr(var, length(var) - 2,2); 
        put substr_from_right=;
run;

/* Output */
last_two_chars=ng

Hopefully this article has been useful for you to learn how to get a substring from the right of a character variable in SAS.

Other Articles You'll Also Like:

  • 1.  Multiple Condition If Statements in SAS Macro Language
  • 2.  SAS scan Function – Return nth Word from Character String
  • 3.  SAS ceil – Round Up to Ceiling of Number in a SAS Data Step
  • 4.  SAS Dollar Format – Formatting Numbers as Dollars in SAS Dataset
  • 5.  SAS month function – Get Month from Date Variable
  • 6.  SAS Delete Dataset – Use PROC Datasets to Delete SAS Files
  • 7.  Get the Row Number in a SAS Data Step with _n_
  • 8.  Do Loop in SAS Macro Language
  • 9.  SAS calculated – Use Columns Created from Select in PROC SQL
  • 10.  SAS year function – Get Year from Date Variable

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy