• 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 find() Function – Check if Substring is in Character Variable

SAS find() Function – Check if Substring is in Character Variable

April 10, 2022 Leave a Comment

The SAS find() function allows us to check if a given substring of characters is in a character variable in a SAS data step.

data k;
    a = 'this is a string with some characters';
    b = find(a,"str");
    put b=;
run;

/* Output: */
b=11

When working with datasets which contain string and character variables, being able to check if a string contains another string can be very useful.

The SAS find() function allows us to check and see if a character variable contains a given substring of characters.

find() takes in two required arguments, the character variable you want to check and a given substring of characters.

The SAS find() function returns the position of where the substring occurs in the character variables, where 1 is the first position.

Below is a simple example showing how you could use find() to find characters in a string and get the position of the characters.

data k;
    a = 'this is a string with some characters';
    b = find(a,"str");
    put b=;
run;

/* Output: */
b=11

If the given substring is not in the string, then find() returns 0.

data k;
    a = 'this is a string with some characters';
    b = find(a,"other");
    put b=;
run;

/* Output: */
b=0

Checking if Substring is in String with SAS find() Function

The SAS find() function has a few optional arguments which allow us to modify the behavior of the returned value.

First, we can pass ‘i’ to find() to ignore the case of the string.

For example, if our string has a mix of uppercase and lowercase characters, then passing ‘i’ could be beneficial.

Below is an example of how to use find() ignoring the case of the characters.

data k;
    a = 'this is a string with some characters';
    b = find(a,"IS",'i');
    put b=;
run;

/* Output: */
b=6

Another modification you can make is if you want to remove the trailing and leading blanks before determining the position of a substring.

You can pass ‘t’ to remove trailing and leading blanks before finding the position of the substring.

Below is an example of using the ‘t’ modifier with the SAS find() function.

data k;
    a = '           this is a string with some leading and trailing blanks            ';
    b = find(a,"is",'t');
    put b=;
run;

/* Output: */
b=6

Finally, we can adjust the starting position of where we start the search of the string. By default, find() starts from the beginning of the string and works right.

But you can start from the end of the string and work left, or start from some other position and work right.

If you pass a number greater than 0, find() will start at that position and work right. If you pass a negative number, find() will start at that position and work left.

By using the ‘startpos’ argument, you can skip over potentially unwanted matches for find().

Below is an example of using the ‘startpos’ argument with find().

data k;
    a = 'this is a string with some characters';
    b = find(a,"th",10);
    put b=;
run;

/* Output: */
b=20

Hopefully this article has been useful for you to learn how to use the SAS find() function in a SAS data step.

Other Articles You'll Also Like:

  • 1.  SAS trim – Remove All Trailing Blanks from String Variable in Data Step
  • 2.  SAS rename Statement – How to Rename Variables in a Data Step
  • 3.  SAS scan Function – Return nth Word from Character String
  • 4.  SAS year function – Get Year from Date Variable
  • 5.  SAS let – Create User-Defined Macro Variables in Your SAS Code
  • 6.  SAS Month Year Format monyyw.
  • 7.  SAS Power Function – Exponentiate Numbers with ** in a Data Step
  • 8.  SAS ceil – Round Up to Ceiling of Number in a SAS Data Step
  • 9.  SAS left() Function – Left Align Character Variables in Data Step
  • 10.  SAS substr() Function – Get Substring from Character 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

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