Modifying Character Values with Functions (continued)

SCAN versus SUBSTR

The SUBSTR function is similar to the SCAN function. Let's briefly compare the two. Both the SCAN and SUBSTR functions can extract a substring from a character value:

  • SCAN extracts words within a value that is marked by delimiters.

  • SUBSTR extracts a portion of a value by starting at a specified location.
The SUBSTR function is best used when you know the exact position of the string that you want to extract from the character value. The string does not need to be marked by delimiters. For example, the first two characters of the variable ID identify the class level of college students. The position of these characters does not vary within the values of ID.

SAS Data

Name ID
Trentonson, Matthew Robert SO45467
Truell, Marcia Elizabeth SR32881


The SUBSTR function is the best choice to extract class level information from ID. By contrast, the SCAN function is best used when
  • you know the order of the words in the character value
  • the starting position of the words varies
  • the words are marked by some delimiter.