scan function sas

SAS SCAN Function: The Ultimate Guide to Split Strings & Extract Words

By Rachel Smith
AD

The SAS SCAN function is one of those deceptively simple tools that can dramatically change how you parse text inside your data step. At its core, it extracts a specific word from a character string based on delimiters, but its real power appears when you deal with inconsistent data, embedded codes, or multi-part fields that were never meant to live in a single column.

Whether you are splitting full names, isolating file extensions, pulling apart pipe-delimited logs, or cleaning customer addresses, the SCAN function in SAS gives you a fast and readable way to work with substrings. Instead of counting positions manually or nesting multiple SUBSTR calls, you can tell SAS which word you want and let SCAN handle the rest.

Understanding the SAS SCAN Function

The SAS SCAN function returns the nth word from a character string. A word is defined as a sequence of characters between delimiters, and delimiters are the characters that separate one word from another. By default, SAS considers blanks, commas, periods, and several other common punctuation marks to be delimiters, which makes SCAN immediately useful for everyday text parsing.

AD
8 Ways to count the number of observations in a SAS dataset.
8 Ways to count the number of observations in a SAS dataset.
a black screen with white text that reads,'i am not sure what this is
a black screen with white text that reads,'i am not sure what this is
the flow diagram for an application with different types of data and numbers, including two lines
the flow diagram for an application with different types of data and numbers, including two lines

You can use SCAN in a DATA step, inside PROC SQL, or even in macro code through the %SCAN macro function. Because it is a native SAS function, it works efficiently across large tables and does not require external parsing libraries. This combination of simplicity and power is why the SCAN function remains a staple in SAS programming for data preparation and transformation.

What the SCAN Function Does in SAS

SCAN scans a string from left to right when the count argument is positive, and from right to left when the count argument is negative. For example, SCAN('SAS programming is powerful', 3) returns the third word, which is 'is'. If you use a negative count such as SCAN('SAS programming is powerful', -1), SAS starts from the end and returns 'powerful'. This bidirectional behavior makes SCAN useful for extracting both leading identifiers and trailing values.

SAS Loops – Understanding Leave and Continue
SAS Loops – Understanding Leave and Continue
SAS User's Guides
SAS User's Guides
How to know some of the Important Transaction Code for Finance in SAP ECC and SAP S/4HANA?
How to know some of the Important Transaction Code for Finance in SAP ECC and SAP S/4HANA?

If the count exceeds the number of words in the string, SCAN returns a blank value. A count of zero also returns a blank. These edge cases are important when your data contains missing or incomplete segments, because they let you detect when a requested word simply does not exist. You can then apply conditional logic to handle those rows instead of letting unexpected blanks flow into later calculations.

Basic Syntax and Core Arguments

The basic syntax is SCAN(string, count, character-list, modifier). The string argument is the character variable or literal you want to parse. The count argument is the integer position of the word you want to return. The character-list argument is optional and lets you define your own delimiters. The modifier argument is also optional and changes how SAS treats consecutive delimiters or other parsing details.

AD

If you omit the character-list, SAS uses its default delimiter set, which includes blank, comma, period, and other common punctuation. If you supply your own list, only those characters are treated as delimiters. For instance, SCAN('2024-05-17', 2, '-') returns '05', and SCAN('Doe,John', 2, ',') returns 'John'. This makes the SCAN function especially handy for dates, names, and concatenated codes that use a consistent separator.

What you do when in SAP you cannot enter the Transaction Code as the Command Bar or OK Code is Missing?
What you do when in SAP you cannot enter the Transaction Code as the Command Bar or OK Code is Missing?
an image of a computer screen with the words map on it and some other symbols
an image of a computer screen with the words map on it and some other symbols
In SAP how to find a Table behind a Transaction Code
In SAP how to find a Table behind a Transaction Code
How to Find User Exits for SAP Program or Transaction Code
How to Find User Exits for SAP Program or Transaction Code
التحليل الإحصائي ببرنامج SAS
التحليل الإحصائي ببرنامج SAS

Advanced Techniques and Practical Applications of SCAN in SAS

Once you move beyond basic word extraction, the SCAN function becomes a versatile string manipulation tool. You can combine it with functions like CAT, CATX, COMPRESS, and TRANWRD to normalize messy values before or after extraction. You can also use it inside iterative loops to walk through every word in a long text field, which is common when parsing keywords, tags, or free-form comments.

Another reason SCAN is so widely used is that it handles both character variables and string literals without any special setup. That flexibility means you can prototype a parsing rule on a single value and then apply it to an entire column with confidence. As your data grows, the same SCAN expression continues to work without redesign, which keeps your SAS code maintainable and easier to debug.

AD

Using SCAN with Multiple Delimiters and Modifiers

Real-world data rarely uses just one delimiter. A field might contain commas, pipes, spaces, and colons all at once. The character-list argument accepts multiple delimiter characters, so you can tell SAS to treat any of them as a separator. For example, SCAN('2024-05-17 14:30', 3, '- :') returns '17' because the hyphen, blank, and colon are all considered delimiters in that call.

The modifier argument adds another layer of control. The 'm' modifier is commonly used to treat multiple consecutive delimiters as a single delimiter, which is helpful when your data contains repeated commas, double pipes, or extra spaces. For example, SCAN('a||b||c', 2, '|', 'm') returns 'b' and ignores the empty segment caused by the double pipe. Without that modifier, empty tokens can shift the word count and produce unexpected results, so the 'm' modifier is often essential for robust parsing.

Common Pitfalls and Performance Considerations

One frequent pitfall is forgetting that SCAN is case-sensitive when you define custom delimiters. If you specify 'a' as a delimiter, it will not treat 'A' as a delimiter. To handle both cases, include both characters in the character-list, or normalize the string with UPCASE or LOWCASE before scanning. Another pitfall is assuming that leading or trailing delimiters are harmless. They can create empty words, so use the 'm' modifier or strip the string first with STRIP or COMPRESS.

AD

Performance is rarely a problem for occasional SCAN calls, but high-volume DATA steps can benefit from a few optimizations. If you need to extract many words from the same string, consider using CALL SCAN, which returns the position and length of a word and can be more efficient than calling SCAN repeatedly. Also keep your character-list as short as practical, and avoid unnecessary function nesting inside the SCAN call. Clean, targeted expressions help SAS process large tables faster while keeping your logic transparent.

As you work with the SAS SCAN function, experiment with negative counts, custom delimiter lists, and modifiers to see how it handles your real-world data. A few well-placed SCAN calls can replace dozens of lines of manual string parsing, leaving your code cleaner and your data more reliable for downstream reporting and analysis.

AD