How can the FINDW function be used in SAS? 2

How can the FINDW function be used in SAS?

The FINDW function in SAS is a powerful tool that allows users to search for specific words or patterns within a text string. It is particularly useful in data manipulation and analysis tasks, as it can be used to locate and extract specific information from a larger dataset. This function can also be used to perform pattern matching, making it an essential tool for data cleaning and quality control. Additionally, the FINDW function can be applied in various SAS procedures, such as data step programming and SQL statements, allowing for its versatile usage in different tasks. Overall, the FINDW function is a valuable feature in SAS that aids in efficiently managing and analyzing data.

Use the FINDW Function in SAS


You can use the FINDW function in SAS to return the position of the first character of a word that occurs within a string.

This function uses the following basic syntax:

FINDW(string, word)

where:

  • string: The string to analyze
  • word: The word to search for within string

The following example shows how to use this function in practice.

Example: Using the FINDW Function in SAS

Suppose we have the following dataset in SAS that contains a column of phrases:

/*create dataset*/
data original_data;
    input phrase $40.;
    datalines;
A pig is my favorite animal
My name is piglet
Pigs are so cute
Here is a baby pig
His name is piggie
;
run;

/*view dataset*/
proc printdata=original_data;

We can use the FINDW function to search for the position of the first occurrence of the word ‘pig’ in the phrase column:

/*find position of first occurrence of 'pig' in phrase column*/
data new_data;
    set original_data;
    findw_pig = findw(phrase, 'pig');
run;

/*view results*/
proc printdata=new_data;

The new column called findw_pig displays the position of the first occurrence of the word ‘pig’ in the phrase column.

If the word ‘pig’ never occurs in the phrase column then the FINDW function simply returns a value of 0.

For example, from the output we can see:

The position of the first occurrence of the word ‘pig’ in the first phrase is 3.

And so on.

The Difference BETWEEN FIND and FINDW Functions

The FIND function in SAS returns the position of the first occurrence of a particular substring in another string.

By contrast, the FINDW function returns the position of the first occurrence of a particular word in another string.

By definition, a word must have a space before and after it.

The following example illustrates the difference between the FIND and FINDW functions:

/*create new dataset*/
data new_data;
    set original_data;
    find_pig = find(phrase, 'pig');
    findw_pig = findw(phrase, 'pig');
run;

/*view new dataset*/
proc printdata=new_data;

FIND vs. FINDW functions in SAS

The find_pig column displays the position of the first occurrence of the substring ‘pig’ in the phrase column.

The findw_pig column displays the position of the first occurrence of the word ‘pig’ in the phrase column.

The following tutorials explain how to use other common functions in SAS:

Cite this article

stats writer (2024). How can the FINDW function be used in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-findw-function-be-used-in-sas/

stats writer. "How can the FINDW function be used in SAS?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-findw-function-be-used-in-sas/.

stats writer. "How can the FINDW function be used in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-findw-function-be-used-in-sas/.

stats writer (2024) 'How can the FINDW function be used in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-findw-function-be-used-in-sas/.

[1] stats writer, "How can the FINDW function be used in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can the FINDW function be used in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top