3.5.8.14 Lookup


Description

This function searches for a string pattern str within a vector vs and, if found, returns a value from vector vref with the same index. Vector vref may be contain numeric or string values. By default, the function searches vs for the full string (option = 2). If no match if found and vref is numeric, the function returns a missing value ("--"); when vref contains strings, an empty string is returned.

The function will return the closest match, using option =0 or =1, but Lookup() does not support wildcard characters.

This function also searches for a numeric value dd within a vector vd and, returns a value from vector vref with the same index. By default, the function seaches vd for the closest value (option = 2). If option = 0, it searchs for the closest value smaller than dd. If option = 1, it searchs for the closest value larger than dd.

Syntax

If vref contains numeric values,

 double Lookup(string str$, vector<string> vs, vector vref[, int option, int Case])

If vref contains string values,

 string Lookup(string str$, vector<string> vs, vector<string> vref[, int option, int Case])$

If search a numeric vector vd,

 double Lookup(double dd, vector vd, vector vref[, int option, int Case])

Parameters

str

is the string pattern to find in vector vs.

vs

is the vector in which string pattern str is looked up.

dd

is the double value to find in vector vd.

vd

is the vector in which value dd is looked up.

vref

is the reference vector whose nth value will be returned. n is the index of string str within vector vs.

option

[optional] integer that specifies how the string is matched:
  • option = 2 (default), searches for a string that fully matches (see Case).
  • option = 0, vector vs is sorted in increasing order. If str is not found, the function will get the index of the nearest value smaller than str.
  • option = 1, vector vs is sorted in decreasing order. If str is not found, the function will get the index of the nearest value larger than str.

Case

[optional] integer that specifies whether the search is case sensitive.
  • case = 0 (default), the search is not case-sensitive.
  • case = 1, the search is case-sensitive.

Return

If str is found in vs, returns a value in vector vref of the same row index.

If option = 2, and str is not found, returns an empty string or missing value.

If option = 0 and str is not found, returns the value corresponding to the nearest smaller value of str. If no smaller value of str exists, returns missing value (numeric) or empty string (string).

If option = 1 and str is not found, returns the value corresponding to the nearest larger value of str. If no larger value of str exists, returns missing value (numeric) or empty string (string).

Example

range aa=1!col(A);
range bb=1!col(B);
string str1$ = Lookup("FSA", aa, bb)$;
str1$=;