INDEXW Function

Searches a character expression for a string that is specified as a word, and returns the position of the first character in the word.

Category: Character
Restriction: I18N Level 0 functions are designed for use with Single Byte Character Sets (SBCS) only.

Syntax

Required Arguments

source

specifies a character constant, variable, or expression to search.

excerpt

specifies a character constant, variable, or expression to search for in source. SAS removes leading and trailing delimiters from excerpt.

Optional Argument

delimiter

specifies a character constant, variable, or expression containing the characters that you want INDEXW to use as delimiters in the character string. The default delimiter is the blank character.

Details

The INDEXW function searches source, from left to right, for the first occurrence of excerpt and returns the position in source of the substring's first character. If the substring is not found in source, then INDEXW returns a value of 0. If there are multiple occurrences of the string, then INDEXW returns only the position of the first occurrence.
The substring pattern must begin and end on a word boundary. For INDEXW, word boundaries are delimiters, the beginning of source, and the end of source. If you use an alternate delimiter, then INDEXW does not recognize the end of the text as the end data.
INDEXW has the following behavior when the second argument contains blank spaces or has a length of 0:
  • If both source and excerpt contain only blank spaces or have a length of 0, then INDEXW returns a value of 1.
  • If excerpt contains only blank spaces or has a length of 0, and source contains character or numeric data, then INDEXW returns a value of 0.

Comparisons

The INDEXW function searches for strings that are words, whereas the INDEX function searches for patterns as separate words or as parts of other words. INDEXC searches for any characters that are present in the excerpts. The FINDW function provides more options.

Examples

Example 1: Table of SAS Examples

The following SAS statements produce these results.
SAS Statement
Result
s='asdf adog dog';
p='dog  ';
x=indexw(s,p);
put x;
 
11
s='abcdef x=y';
p='def';
x=indexw(s,p);
put x;
 
0
x="abc,def@ xyz";
abc=indexw(x, " abc ", "@");
put abc;
 
0
x="abc,def@ xyz";
comma=indexw(x, ",", "@");
put comma;
 
0
x='abc,def% xyz';
def=indexw(x, 'def', '%,');
put def;
 
5
x="abc,def@ xyz";
at=indexw(x, "@", "@");
put at;
 
0
x="abc,def@ xyz";
xyz=indexw(x, " xyz", "@");
put xyz;
 
9
c=indexw(trimn(' '), '        ');
1
g=indexw('  x  y  ', trimn(' '));
0

Example 2: Using a Semicolon (;) As the Delimiter

The following example shows how to use the semicolon delimiter in a SAS program that also calls the CATX function. A semicolon delimiter must be in place after each call to CATX, and the second argument in the INDEXW function must be trimmed or searches will not be successful.
data temp;
   infile datalines;
   input name $12.;
   datalines;
abcdef
abcdef
;
run;
data temp2;
   set temp;
   format name_list $1024.;
   retain name_list ' ';
   exists=indexw(name_list, trim(name), ';');
   if exists=0 then do
      name_list=catx(';', name_list, name)||';' ;
	  name_count +1;
      put '-------------------------------';
      put exists= ;
      put name_list= ;
      put name_count= ; 
	  end;
	run;
Output from Using a Semicolon As the Delimiter
-------------------------------
exists=0
name_list=abcdef;
name_count=1
In this example, the first time CATX is called name_list is blank and the value of name is 'abcdef'. CATX returns 'abcdef' with no semicolon appended. However, when INDEXW is called the second time, the value of name_list is 'abcdef' followed by 1018 (1024–6) blanks, and the value of name is 'abcdef' followed by six blanks. Because the third argument in INDEXW is a semicolon (;), the blanks are significant and do not denote a word boundary. Therefore, the second argument cannot be found in the first argument.
If the example has no blanks, the behavior of INDEXW is easier to understand. In the following example, we expect the value of x to be 0 because the complete word ABCDE was not found in the first argument:
x = indexw('ABCDEF;XYZ', 'ABCDE', ';');
The only values for the second argument that would return a nonzero result are ABCDEF and XYZ.

Example 3: Using a Space As the Delimiter

The following example uses a space as a delimiter:
data temp;
   infile datalines;
   input name $12.;
   datalines;
abcdef
abcdef
;
run;
data temp2;
   set temp;
   format name_list $1024.;
   retain name_list ' ';
   exists=indexw(name_list, name, ' ');
   if exists=0 then do
      name_list=catx(' ', name_list, name) ;
	  name_count +1;
      put '-------------------------------';
      put exists= ;
      put name_list= ;
      put name_count= ; 
	  end;
run;
Output from Using a Space as the Delimiter
-------------------------------
exists=0
name_list=abcdef
name_count=1