SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Search||next

Transforming Data with SAS Functions
Modifying Character Values with Functions

 
SCAN Function

The SCAN function enables you to separate a character value into words and to return a specified word. Let's look at the following example to see how the SCAN function works.

The data set Hrd.Temp stores the names of temporary employees in the variable Name. The Name variable contains the employees' first, middle, and last names.

SAS Data Set Hrd.Temp
Agency ID Name
Administrative Support, Inc. F274 CICHOCK, ELIZABETH MARIE
Administrative Support, Inc. F101 BENINCASA, HANNAH LEE
OD Consulting, Inc. F054 SHERE, BRIAN THOMAS


However, you would like to separate the value of Name into three variables, one variable to store the first name, one to store the middle name, and one to store the last name. You can create these new variables using the SCAN function.

SAS Data Set Hrd.Temp
Agency ID LastName FirstName MiddleName
Administrative Support, Inc. F274 CICHOCK ELIZABETH MARIE
Administrative Support, Inc. F101 BENINCASA HANNAH LEE
OD Consulting, Inc. F054 SHERE BRIAN THOMAS


Specifying Delimiters

The SCAN function uses delimiters, which are characters specified as word separators, to separate a character string into words. For example, if you are working with the character string below and you specify the comma as a delimiter, the SCAN function separates the string into three words.

comma delimiter

Then the function returns whichever word you specify. In this example, if you specify the third word, the SCAN function returns the word HIGH.

Here's another example. Once again, let's use the comma as a delimiter, and specify that the third word be returned.

     209 RADCLIFFE ROAD, CENTER CITY, NY, 92716

In this example, the word returned by the SCAN function is NY.


Specifying Multiple Delimiters

When using the SCAN function, you can specify as many delimiters as needed to correctly separate the character expression. When you specify multiple delimiters, SAS software uses all of the delimiters as word separators. For example, if you specify the slash and the hyphen as delimiters, the SCAN function separates the following text string into three words:

multiple delimiters

The SCAN function treats two or more contiguous delimiters, such as the parentheses and slash below, as one delimiter. Also, leading delimiters have no effect.

contiguous delimiters


Default Delimiters

If you do not specify delimiters when using the SCAN function, default delimiters are used. The default delimiters are

     blank . < ( + | & ! $ * ) ; ^ - / , %


|next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement