• 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 / 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 Less Than or Equal to with LE or <=
  • 2.  SAS Dollar Format – Formatting Numbers as Dollars in SAS Dataset
  • 3.  SAS Uppercase – Make String Letters Uppercase with SAS upcase function
  • 4.  SAS If Then Statements with Multiple Variables
  • 5.  SAS Remove Formats from Dataset with PROC DATASETS
  • 6.  SAS Not In – How to Check if Variable is Not in List of Values
  • 7.  SAS floor – Round Down to Floor of Number in a SAS Data Step
  • 8.  SAS round – Rounding Numbers in a SAS Data Step
  • 9.  SAS intnx – Add or Subtract Time from Date Variables in SAS Data Step
  • 10.  SAS Remove Labels from Dataset with PROC DATASETS

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