• 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 / 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.  catx SAS – Concatenate String Variables with Delimiter in SAS Data Step
  • 2.  Date Format ddmmmyyyy in SAS
  • 3.  intcx SAS – Find Time Periods Between Two Dates in SAS Data Step
  • 4.  SAS scan Function – Return nth Word from Character String
  • 5.  SAS Lowercase – Make String Lowercase with SAS lowcase function
  • 6.  SAS where in – Subset Data by Multiple Values in Data Step
  • 7.  =">SAS Greater Than or Equal to with GE or >=
  • 8.  SAS substr() Function – Get Substring from Character Variable
  • 9.  SAS strip – Remove All Leading and Trailing Blanks from String Variable
  • 10.  SAS ceil – Round Up to Ceiling of Number in a SAS 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