The SAS IN operator tests whether a value matches any item in a list, returning a true or false result that you can use in IF, WHERE, and SELECT statements. For example, if age in (18, 21, 25) then ... checks if age equals 18, 21, or 25. It is a concise alternative to a series of OR comparisons and works with both numeric and character values.
What the IN Operator Does
The IN operator is a logical operator that evaluates to true when the value on its left appears in the list on its right. You can use it in any expression where a logical comparison is allowed, such as in IF statements, WHERE clauses, and CASE expressions. It is equivalent to writing multiple OR conditions but is shorter and less error-prone.
For numeric values, the list is written with commas, like in (1, 2, 3). For character values, each item must be quoted, like in ('A', 'B', 'C'). The operator is case-sensitive for character values by default, so 'a' is not the same as 'A' unless you use the UPCASE or LOWCASE function.
Basic Syntax and Placement
The general syntax is:
value IN (item1, item2, ...);
You can use it in a DATA step with an IF statement:
data want;
set have;
if age in (18, 21, 25) then flag = 1;
run;
Or in a WHERE clause to filter rows:
proc print data=have;
where state in ('NY', 'CA', 'TX');
run;
It also works in PROC SQL:
proc sql;
select * from have
where age in (18, 21, 25);
quit;
You can also use the NOT IN operator to test for non-membership, but be careful with missing values, as discussed later.
Numeric vs. Character Lists
For numeric values, the list items are numbers without quotes. For character values, each item must be enclosed in quotes. The list can contain up to 255 items in a single IN expression in the DATA step, though you can work around that limit by using a format or a custom macro.
Example with character values:
if product in ('Laptop', 'Mouse', 'Keyboard') then category = 'Computer';
If you need to compare case-insensitively, use the UPCASE function on both sides:
if upcase(state) in ('NY', 'CA') then ...;
Using IN with Formats
A common alternative to a long IN list is to define a user-defined format with PROC FORMAT. This is especially useful when the list is large or changes frequently. For example:
proc format;
value $region 'NY','CA','TX' = 'West'
'FL','GA' = 'South';
run;
data want;
set have;
if put(state, $region.) in ('West', 'South') then ...;
run;
Formats can handle more than 255 items and are easier to maintain. They also allow you to group values into categories, which is more flexible than a simple membership test.
Common Pitfalls and Misconceptions
One common mistake is using IN with missing values. If a variable has a missing value, var in (1, 2) will return false (not missing) because missing does not equal any item. However, var not in (1, 2) will return true for a missing value, which may not be intended. To exclude missing values, add an explicit check: if var not in (1, 2) and var ne .
Another misconception is that IN works with ranges like in (1:10). It does not. You must list each value separately or use a comparison like var ge 1 and var le 10. Also, the list items are evaluated as expressions, so you can use variables in the list, but they are evaluated once at the time the IN expression is evaluated.
Case sensitivity is another frequent issue. By default, character comparisons in SAS are case-sensitive, so 'a' is not equal to 'A'. Use UPCASE or LOWCASE to normalize.
Performance Considerations
For small lists, IN is efficient. For very large lists (hundreds of items), a format or a hash lookup may be faster. In a WHERE clause, SAS may optimize the IN operator into an index scan if the variable is indexed. In a DATA step, the IN operator is evaluated for each observation, so if the list is long, it may be slower than a format-based approach.
If you have a list stored in a macro variable, you can use it directly:
%let list = 1, 2, 3;
data want;
set have;
if age in (&list.) then ...;
run;
This works because the macro variable is resolved before the DATA step is compiled. Be careful with macro quoting if the list contains special characters.
Alternatives to IN
Several alternatives exist depending on the situation:
- OR operators: Simple but verbose for long lists.
- Formats: Best for large or changing lists, and for grouping values.
- Hash tables: Efficient for very large lookup sets, especially in DATA step merges.
- PROC SQL with a subquery: Useful when the list comes from another table.
For example, to check membership against a table of valid IDs:
proc sql;
select * from have
where id in (select id from valid_ids);
quit;
Frequently Asked Questions
Can I use IN with date values?
Yes, but dates must be numeric SAS date values. You can use a date literal like '01JAN2024'd inside the list: if date in ('01JAN2024'd, '15JAN2024'd) then ...;.
Does IN work with missing values?
Yes, but a missing value will never match a non-missing item, so var in (.) checks for missing. However, var not in (1,2) returns true for missing, which can be surprising. Add an explicit missing check if needed.
What is the maximum number of items in an IN list?
In the DATA step, the limit is 255 items per expression. In PROC SQL, the limit is higher (often 1000 or more), but it depends on the version. For larger lists, use a format or a hash table.
Is IN case-sensitive?
Yes, character comparisons are case-sensitive by default. Use UPCASE on both sides to make it case-insensitive.
Can I use IN in a macro?
Yes, you can use %sysfunc with the index function or use the macro language to check membership, but the simplest is to generate the IN expression with a macro variable as shown above.
Conclusion
The IN operator is a straightforward way to test membership in a list. It works in DATA steps, WHERE clauses, and PROC SQL. Remember the case sensitivity, missing value behavior, and the 255-item limit. For larger or dynamic lists, consider formats or hash tables. With these details, you can use IN effectively and avoid common mistakes.
Related
- Sitting Balance Score Calculator: How It Works
- BITAND Function in Google Sheets: Syntax & Examples
- Data Entry Clerk Job: Duties, Skills, Pay
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Can You Insert a Horizontal Line in Word?Yes. In Microsoft Word for Microsoft 365 on Windows, you can insert a horizontal line from Home >…
- Excel File Format or File Extension Not Valid? FixesIf Excel says the file format or file extension is not valid, it usually means the workbook’s contents…
- How to Remove Text to Columns Formatting in ExcelTo remove a Text to Columns split in Excel, press Ctrl+Z immediately after applying it. If you have…
- How to Add an Attachment to a Word Mail MergeTo add a different attachment to each Word mail-merge email, put each recipient’s file path in your data…
- How to Center a Number in an Excel Cell: Quick StepsTo center a number in Excel, select the cell or range, then choose Home > Alignment > Center.…
- How to Change the Outlook View to Show Sender NamesTo show who sent each email in Outlook for Windows classic, open the folder, choose View > Change…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.