• 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 where in – Subset Data by Multiple Values in Data Step

SAS where in – Subset Data by Multiple Values in Data Step

April 21, 2022 Leave a Comment

When filtering a SAS dataset, you can filter by multiple values with the in operator in a where statement.

data want;
   set have;
   where variable_a in (1, 2, 3);
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 by multiple values.

By using the SAS in operator combined with a where statement, you can subset data based on multiple values.

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    TX     no
    dog  female   80    4    FL     no
    ;
run;

Let’s say that we only want to keep records where the state is Texas (“TX”) or Florida (“FL”).

Below is a example of how to use in in a where statement in SAS.

data want;
	set have;
        where state in ("TX", "FL");
run;

The resulting dataset is below.

sas where in

Using Not In to Filter a Dataset in SAS

If you want to create a subset of data based on a variable which has values not in a list of values, we can use the SAS not in operator.

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

Let’s say that we only want to keep records where the state is not Texas (“TX”) or Florida (“FL”).

Below is a example of how to use in in a where statement in SAS.

data want;
	set have;
        where state not in ("TX", "FL");
run;

The resulting dataset is below.

sas where not in

Hopefully this article has been useful for you to learn how to use the in operator in a where statement in SAS.

Other Articles You'll Also Like:

  • 1.  Using SAS to Sum by Group with PROC MEANS
  • 2.  SAS substr() Function – Get Substring from Character Variable
  • 3.  SAS Not In – How to Check if Variable is Not in List of Values
  • 4.  SAS prxmatch – Find Pattern in String Using Regex (Regular Expression)
  • 5.  SAS let – Create User-Defined Macro Variables in Your SAS Code
  • 6.  SAS Month Year Format monyyw.
  • 7.  SAS Not Equal – Check if a Variable is Not Equal to Another in Data Step
  • 8.  SAS left() Function – Left Align Character Variables in Data Step
  • 9.  SAS Remove Labels from Dataset with PROC DATASETS
  • 10.  catx SAS – Concatenate String Variables with Delimiter in 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