• 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 Not In – How to Check if Variable is Not in List of Values

SAS Not In – How to Check if Variable is Not in List of Values

April 20, 2022 Leave a Comment

To check if a variable’s value is not in a list of values, we can use the SAS not operator in combination with the in operator.

data k;
    a = 4;
    if a NOT IN (1, 2, 3) then put 'a is not in (1, 2, 3)';
run;

/* Output: */
 a is not in (1, 2, 3)

When working in SAS, logical operators allow us to control the flow of our data.

There are many different logical operators which allow us to perform checks on the values of the variables we are working with.

One such check is to see if a variable is in or not in a list of values.

If you want to check against a collection of values, you can use the SAS not operator in combination with the in operator.

not in checks if the variable is not equal to each of the values of your array.

Below is an example of how to use not in in a SAS data step to see if a variable value is not in a list of values.

data k;
    a = 4;
    if a NOT IN (1, 2, 3) then put 'a is not in (1, 2, 3)';
run;

/* Output: */
 a is not in (1, 2, 3)

Checking to See if a Value is IN a List of Values in SAS

If you want to check if a value is in a list of a values in SAS, you can use the in operator. The in operator used in a SAS data step is very useful when you want to see if a variable is in an array of values.

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

data k;
    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 create another variable “Region” by using the IN operator. Let’s say that if state is Texas (“TX”) or Florida (“FL”) that “Region is “South” and if it’s not, then “Region” is “Other”.

We can easily do this with the following SAS code:

data m;
	set k;
	if state in ("TX", "FL") then region = "South";
	else region = "Other";
run;

The resulting SAS data set will look like the following:

in in sas data step

Other Articles You'll Also Like:

  • 1.  Using SAS to Sum by Group with PROC MEANS
  • 2.  Get Last Observation of SAS Dataset with end=
  • 3.  SAS let – Create User-Defined Macro Variables in Your SAS Code
  • 4.  mod Function in SAS – Find Remainder of 2 Numbers After Division
  • 5.  SAS tranwrd() Function – Replace Characters in Strings in SAS Data Step
  • 6.  SAS %eval() Function – Evaluate Expressions in SAS Macro
  • 7.  SAS _n_ – How to Use the Automatic Variable _n_ in a Data Step
  • 8.  SAS Delete Dataset – Use PROC Datasets to Delete SAS Files
  • 9.  PROC Format – Create User-Defined Formats for Variables in SAS
  • 10.  SAS right() Function – Right Align Character Variables in 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