Previous Page | Next Page

Functions and CALL Routines

WHICHC Function



Searches for a character value that is equal to the first argument, and returns the index of the first matching value.
Category: Search

Syntax
Arguments
Details
Examples
See Also

Syntax

WHICHC(string, value-1 <, value-2, ...>)


Arguments

string

is a character constant, variable, or expression that specifies the value to search for.

value

is a character constant, variable, or expression that specifies the value to be searched.


Details

The WHICHC function searches the second and subsequent arguments for a value that is equal to the first argument, and returns the index of the first matching value.

If string is missing, then WHICHC returns a missing value. Otherwise, WHICHC compares the value of string with value-1, value-2, and so on, in sequence. If argument value-i equals string, then WHICHC returns the positive integer i. If string does not equal any subsequent argument, then WHICHC returns 0.

Using WHICHC is useful when the values that are being searched are subject to frequent change. If you need to perform many searches without changing the values that are being searched, using the HASH object is much more efficient.


Examples

The following example searches the array for the first argument and returns the index of the first matching value.

data _null_;
   array fruit (*) $12 fruit1-fruit3 ('watermelon' 'apple' 'banana');
   x1=whichc('watermelon', of fruit[*]);
   x2=whichc('banana', of fruit[*]);
   x3=whichc('orange', of fruit[*]);
   put x1= / x2= / x3=;
run;

SAS writes the following output to the log:

x1=1
x2=3
x3=0


See Also

Functions:

WHICHN Function

"The IN Operator in Character Comparisons" in SAS Language Reference: Concepts.

"Using the HASH Object" in SAS Language Reference: Concepts.

Previous Page | Next Page | Top of Page