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: