• 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 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.  SAS %eval() Function – Evaluate Expressions in SAS Macro
  • 2.  SAS year function – Get Year from Date Variable
  • 3.  intcx SAS – Find Time Periods Between Two Dates in SAS Data Step
  • 4.  SAS select when – Evaluating Character Values for Conditional Processing
  • 5.  Round Number to 2 Decimal Places in SAS
  • 6.  SAS rand – Generate Random Numbers in a SAS Data Step
  • 7.  SAS compress – Remove Whitespace and Characters from String
  • 8.  SAS Remove Formats from Dataset with PROC DATASETS
  • 9.  SAS Power Function – Exponentiate Numbers with ** in a Data Step
  • 10.  SAS floor – Round Down to Floor of Number in a 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

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