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:
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.
Hopefully this article has been helpful for you to learn how to use the IN operator in your SAS code.