• 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 contains() – Check if Variable Contains a String in Where Statement

SAS contains() – Check if Variable Contains a String in Where Statement

April 21, 2022 Leave a Comment

In SAS, we can check if a variable contains a specific string with the contains operator in a where statement.

data want;
   set have;
   where variable contains "something";
run;

When working in SAS, the ability to easily be able to create complex filters and get the subsets we desire is valuable.

One such filter which we can create in SAS is filtering a dataset where a variable contains a specific string.

We can check if a variable contains a string in a where statement with the SAS contains() operator.

Let’s say we have following data set which we create with the following data step:

data have;
    input animal_type $ gender $ weight age state $ trained $;
    datalines;
    cat  male     10    1    CA     no
    dog  male     20    4    FL     no
    dog  male     30    5    NY     no
    cat  female   40    3    FL     yes
    cat  female   10    2    NY     yes
    dog  female   20    4    TX     yes
    cat  female   50    6    TX     yes
    dog  male     60    1    CA     no
    dog  male     70    5    NY     no
    cat  female   80    4    FL     yes
    cat  female   90    3    TX     yes
    cat  male     100   2    MN     no
    dog  female   80    4    MN     no
    ;
run;

If we to get a subset of all of the records where the state contains “N”, we can do that in a where statement in the following code.

data want;
   set have;
   where state contains "N";
run;

Below is the resulting dataset.

sas contains

Checking if SAS Variable Contains Multiple Strings

Unfortunately, there is no way that you can check if a variable contains multiple strings with the contains() operator.

In that case, you will have to use the or operator to string together contains() statements.

Let’s say we have the same dataset as above.

If you want to check for states which contains “N” or “L”, we can do so in the following where statement.

data want;
   set have;
   where state contains "N" or state contains "F";
run;

Hopefully this article has been useful for you to learn how to subset a dataset in a SAS data step with the SAS contains() operator.

Other Articles You'll Also Like:

  • 1.  SAS mean() Function – Find Average Across Columns in Data Step
  • 2.  Using SAS to Sum by Group with PROC MEANS
  • 3.  SAS data _null_ – Create a SAS Dataset with No Records and Variables
  • 4.  IN in SAS – Checking if Variable is in Array of Values
  • 5.  mod Function in SAS – Find Remainder of 2 Numbers After Division
  • 6.  SAS Remove Formats from Dataset with PROC DATASETS
  • 7.  SAS _n_ – How to Use the Automatic Variable _n_ in a Data Step
  • 8.  SAS find() Function – Check if Substring is in Character Variable
  • 9.  Set Multiple Datasets in SAS Data Step
  • 10.  Round Number to Nearest Integer in SAS

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