Functions and CALL Routines

FINDC Function



Searches for specific characters that either appear or do not appear within a character string that you specify
Category: Character

Syntax
Arguments
Details
Comparisons
Examples
Example 1: Searching for the Characters h, i, and Blank
Example 2: Searching for the Characters h and i While Ignoring Case
Example 3: Searching for the Characters h and i with Trailing Blanks Trimmed
Example 4: Searching for all Characters, Excluding h, i, H, and I
See Also

Syntax

FINDC(string,characters<,modifiers><,startpos>)
FINDC(string,characters<,startpos><,modifiers>)


Arguments

string

specifies a character constant, variable, or expression that will be searched for characters.

Tip: Enclose a literal string of characters in quotation marks.
characters

is a character constant, variable, or expression that specifies one or more characters to search for in string.

Tip: Enclose a literal string of characters in quotation marks.
modifiers

is a character constant, variable, or expression that specifies one or more modifiers. The following modifiers can be in uppercase or lowercase:

i

ignores character case during the search. If this modifier is not specified, FINDC only searches for character substrings with the same case as the characters in characters.

o

processes characters and modifiers only once, at the first call to this instance of FINDC. Consequently, if you change the value of characters or modifiers in subsequent calls, the change is ignored by FINDC.

t

trims trailing blanks from string and characters.

v

counts only the characters that do not appear in characters.

Tip: If the modifier is a constant, enclose it in quotation marks. Specify multiple constants in a single set of quotation marks. Modifier can also be expressed as a variable or an expression that evaluates to one or more constants.
startpos

is an integer that specifies the position at which the search should start and the direction of the search.


Details

The FINDC function searches string for the first occurrence of the specified characters, and returns the position of the first character found. If the characters are not found in string, FINDC returns a value of 0.

If startpos is not specified, FINDC starts the search at the beginning of the string and searches the string from left to right. If startpos is specified, the absolute value of startpos determines the position at which to start the search. The sign of startpos determines the direction of the search.

When startpos is ... then FINDC ...
greater than 0 starts the search at position startpos and the direction of the search is to the right. If startpos is greater than the length of string, FINDC returns a value of 0.
less than 0 starts the search at position -startpos and the direction of the search is to the left. If -startpos is greater than the length of string, the search starts at the end of string.
equal to 0 returns a value of 0.


Comparisons


Examples


Example 1: Searching for the Characters h, i, and Blank

This example searches for the three characters h, i, and blank. The characters h and i are in lowercase. The uppercase characters H and I are ignored in this search.

data _null_;
   whereishi=0;
   do until(whereishi=0);
      whereishi=findc('Hi there, Ian!','hi ',whereishi+1);
      if whereishi=0 then put "The End";
      else do;
         whatfound=substr('Hi there, Ian!',whereishi,1);
         put whereishi= whatfound=;
      end;
   end;
run;

The following lines are written to the SAS log:

whereishi=2 whatfound=i
whereishi=3 whatfound= 
whereishi=5 whatfound=h
whereishi=10 whatfound= 
The End


Example 2: Searching for the Characters h and i While Ignoring Case

This example searches for the four characters h, i, H, and I. FINDC with the i modifier ignores character case during the search.

data _null_;
   whereishi_i=0;
   do until(whereishi_i=0);
      variable1='Hi there, Ian!';
      variable2='hi';
      variable3='i';
      whereishi_i=findc(variable1,variable2,variable3,whereishi_i+1);
      if whereishi_i=0 then put "The End";
      else do;
         whatfound=substr(variable1,whereishi_i,1);
         put whereishi_i= whatfound=;
      end;
   end;
run;

The following lines are written to the SAS log:

whereishi_i=1 whatfound=H
whereishi_i=2 whatfound=i
whereishi_i=5 whatfound=h
whereishi_i=11 whatfound=I
The End


Example 3: Searching for the Characters h and i with Trailing Blanks Trimmed

This example searches for the two characters h and i. FINDC with the t modifier trims trailing blanks from the string argument and the characters argument.

data _null_;
   whereishi_t=0;
   do until(whereishi_t=0);
      expression1='Hi there, '||'Ian!';
      expression2=kscan('bye or hi',3)||'  ';
      expression3=trim('t   ');
      whereishi_t=findc(expression1,expression2,expression3,whereishi_t+1);
      if whereishi_t=0 then put "The End";
      else do;
         whatfound=substr(expression1,whereishi_t,1);
         put whereishi_t= whatfound=;
      end;
   end;
run;

The following lines are written to the SAS log:

whereishi_t=2 whatfound=i
whereishi_t=5 whatfound=h
The End


Example 4: Searching for all Characters, Excluding h, i, H, and I

This example searches for all of the characters in the string, excluding the characters h, i, H, and I. FINDC with the v modifier counts only the characters that do not appear in the characters argument. This example also includes the i modifier and therefore ignores character case during the search.

data _null_;
   whereishi_iv=0;
   do until(whereishi_iv=0);
      xyz='Hi there, Ian!';
      whereishi_iv=findc(xyz,'hi',whereishi_iv+1,'iv');
      if whereishi_iv=0 then put "The End";
      else do;
         whatfound=substr(xyz,whereishi_iv,1);
         put whereishi_iv= whatfound=;
      end;
   end;
run;

The following lines are written to the SAS log:

whereishi_iv=3 whatfound= 
whereishi_iv=4 whatfound=t
whereishi_iv=6 whatfound=e
whereishi_iv=7 whatfound=r
whereishi_iv=8 whatfound=e
whereishi_iv=9 whatfound=,
whereishi_iv=10 whatfound= 
whereishi_iv=12 whatfound=a
whereishi_iv=13 whatfound=n
whereishi_iv=14 whatfound=!
The End


See Also

Functions:

COUNTC Function

FIND Function

INDEXC Function

VERIFY Function

space
Previous Page | Next Page | Top of Page