close
close
sas scan function example

sas scan function example

2 min read 26-02-2025
sas scan function example

The SAS SCAN function is a powerful tool for parsing text strings, extracting specific parts, and manipulating data within your SAS programs. It's essential for data cleaning, text analysis, and creating more manageable datasets. This article provides a comprehensive guide with practical examples to help you master the SCAN function.

Understanding the SAS SCAN Function

The SCAN function in SAS extracts words or tokens from a character string based on delimiters. It's incredibly versatile, allowing for flexible data manipulation. The basic syntax is:

SCAN(string, n, delimiter)
  • string: The character string you want to parse.
  • n: The position of the word or token you want to extract (starting from 1).
  • delimiter: The characters that separate the words or tokens (default is a blank space).

Basic Examples of the SAS SCAN Function

Let's start with some straightforward examples to illustrate the core functionality.

Example 1: Extracting the First Word

Suppose you have a variable named FullName containing the string "John Doe". To extract the first name ("John"), you'd use:

data names;
  input FullName $20.;
  FirstName = scan(FullName, 1);
  datalines;
John Doe
Jane Smith
Peter Jones
;
run;

This code snippet extracts the first word (before the space) and assigns it to the FirstName variable.

Example 2: Extracting the Second Word

To extract the last name ("Doe"), we change the n parameter to 2:

data names;
  input FullName $20.;
  LastName = scan(FullName, 2);
  datalines;
John Doe
Jane Smith
Peter Jones
;
run;

This neatly separates the last name.

Example 3: Specifying a Delimiter

Consider a string with commas as delimiters: "apples,bananas,oranges". To extract the second item ("bananas"):

data fruits;
  input FruitList $50.;
  Fruit2 = scan(FruitList, 2, ',');
  datalines;
apples,bananas,oranges
grapes,pears,kiwis
;
run;

Here, the comma (,) is explicitly defined as the delimiter.

Advanced Applications of the SCAN Function

The SCAN function's true power lies in its ability to handle complex scenarios.

Example 4: Handling Multiple Delimiters

You can specify multiple delimiters using the %STR() function. For example, to extract words separated by spaces or commas:

data mixedDelimiters;
  input MyString $50.;
  Word1 = scan(MyString, 1, %str( ,)); /* Space or comma */
  datalines;
apples, bananas and oranges
grapes pears,kiwi
;
run;

This demonstrates how to handle both spaces and commas as delimiters.

Example 5: Extracting the Last Word

Extracting the last word requires a slightly different approach. You can use the COUNTW() function to determine the number of words and then extract the last one:

data lastWord;
  input Sentence $100.;
  NumberOfWords = countw(Sentence);
  LastWord = scan(Sentence, NumberOfWords);
  datalines;
This is a sample sentence.
Another example here.
;
run;

This dynamically determines the last word's position.

Example 6: Error Handling

If you attempt to extract a word beyond the number of words in the string, SCAN will return a blank value. It's good practice to check the number of words first using COUNTW() to avoid unexpected results.

Conclusion

The SAS SCAN function is a vital tool for data manipulation and text processing. By understanding its basic and advanced applications, you can significantly improve the efficiency and effectiveness of your SAS programs. Remember to experiment with different delimiters and the n parameter to fully explore its capabilities. Mastering the SCAN function will empower you to tackle a wide range of data cleaning and transformation tasks with confidence.

Related Posts