• 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 substr() Function – Get Substring from Character Variable

SAS substr() Function – Get Substring from Character Variable

April 12, 2022 Leave a Comment

To get a substring of a character variable in a SAS data step, you can use the SAS substr() function. The substr() function takes arguments which define the starting position of the substring and how many characters you want to get from the string.

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

/* Output */
first_two_chars=st

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 character 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.

You can use substr() to extract substrings from strings, and also change the value of strings by changing a specific substring.

Below are some simple examples of extracting substrings from character variables with the SAS substr() variable.

data k;
        var = "this is a string";
        first_two_chars = substr(var, 1, 2); 
        first_five_chars = substr(var, 1, 5); 
        second_five_chars = substr(var, 6, 5); 
        put first_two_chars=;
        put first_five_chars=;
        put second_five_chars=;
run;

/* Output */
first_two_chars=th
first_five_chars=this 
second_five_chars=is a 

Changing Substrings of Character Variables in SAS

With the substr() function, you can also set substrings in strings to different values. If you put substr() on the left side of the equation, you can change the original string.

Below are some examples of how you can change pieces of strings with the SAS substr() function.

data k;
        var = "this is a string";
        substr(var, 1, 4) = "that";
        substr(var, 11, 6) = "puzzle";
        put var=;
run;

/* Output */
var=that is a puzzle

Getting Substrings from the Right of a Character Variable in SAS

You can also use the SAS substr() function to get a substring from the right of a character variable.

If you want to get a substring of a string and start from the end 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 use the SAS substr() function to get substrings from character variables in SAS.

Other Articles You'll Also Like:

  • 1.  intcx SAS – Find Time Periods Between Two Dates in SAS Data Step
  • 2.  SAS Comments – Commenting Your SAS Code the Right Way
  • 3.  SAS ceil – Round Up to Ceiling of Number in a SAS Data Step
  • 4.  catx SAS – Concatenate String Variables with Delimiter in SAS Data Step
  • 5.  SAS Remove Labels from Dataset with PROC DATASETS
  • 6.  SAS _n_ – How to Use the Automatic Variable _n_ in a Data Step
  • 7.  SAS month function – Get Month from Date Variable
  • 8.  Date Format ddmmmyyyy in SAS
  • 9.  SAS Not Equal – Check if a Variable is Not Equal to Another in Data Step
  • 10.  Do Loop in SAS Macro Language

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