• 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 / IN in SAS – Checking if Variable is in Array of Values

IN in SAS – Checking if Variable is in Array of Values

January 17, 2022 Leave a Comment

Being able to see if a value is in an array can be very useful when programming. Using IN in SAS is very useful and allows us as SAS programmers to write concise code.

In this post, you will learn how to use IN in SAS, and learn how you can use IN with SAS data steps, SAS Macro Language and with PROC SQL.

First, let’s see how IN is used in a data step.

Using IN in a SAS Data Step

Using IN 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

You can also use NOT in combination with IN to return a boolean value for variable values which are not in an array of values.

Below is an example of NOT IN in SAS.

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

Using IN with SAS Macro Language

Using the SAS Macro language allows us to create complex code which enables use to do amazing things in our programs.

Unfortunately, IN does not exist by default as a function in the SAS Macro language. However, we can use in in the following way if we turn on some options in our code.

Let’s say I have an array of values and want to see if a macro variable is in that array of values.

I need to turn on the “minoperator” option and set the “mindelimiter” option to be able to use IN.

options minoperator mindelimiter=',';
%let var1 = Apple;
%let var2 = Blueberry;

%macro checkVar(var);
    %if &var in (Apple, Banana, Pear, Peach) %then %do;
    	%put Yes!;
    %end; 
    %else %do;
    	%put No!;
    %end;
%mend;

%checkVar(&var1);
%checkVar(&var2);

As we can see below in the SAS log, this works as expected.

 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 68         
 69         
 70         %macro checkVar(var);
 71             %if &var in (Apple, Banana, Pear, Peach) %then %do;
 72             %put Yes!;
 73             %end;
 74             %else %do;
 75             %put No!;
 76             %end;
 77         %mend;
 78         
 79         %checkVar(&var1);
 Yes!
 80         %checkVar(&var2);
 No!
 81         
 82         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 92       

Using IN with PROC SQL in SAS

Finally, let’s learn how to use IN in a PROC SQL statement. Typically, we would want to use IN in a where statement to filter our dataset.

Let’s say we have the same dataset above and want to filter the data to create a dataset with only animals from Texas and Florida in it.

We can do so easily with the following SAS code:

proc sql;
	create table south as
	select *	   
	from k
	where state in ("TX", "FL")
;
quit;

As you can see below, PROC SQL filters out the animals from the other states.

in in sas proc sql

Hopefully this article has been helpful for you to learn how to use the IN operator in your SAS code.

Other Articles You'll Also Like:

  • 1.  catx SAS – Concatenate String Variables with Delimiter in SAS Data Step
  • 2.  SAS right() Function – Right Align Character Variables in Data Step
  • 3.  SAS call symput – Create Macro Variables in Data Step
  • 4.  SAS Power Function – Exponentiate Numbers with ** in a Data Step
  • 5.  SAS Uppercase – Make String Letters Uppercase with SAS upcase function
  • 6.  Round Number to 2 Decimal Places in SAS
  • 7.  SAS trim – Remove All Trailing Blanks from String Variable in Data Step
  • 8.  SAS where in – Subset Data by Multiple Values in Data Step
  • 9.  SAS include – Execute Code from Other Files in SAS with %include
  • 10.  SAS ceil – Round Up to Ceiling 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

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