ANYFIRST Function

Searches a character string for a character that is valid as the first character in a SAS variable name under VALIDVARNAME=V7, and returns the first position at which that character is found.

Categories:Character
CAS
Restriction:This function is assigned an I18N Level 2 status, and is designed for use with SBCS, DBCS, and MBCS (UTF8). For more information, see Internationalization Compatibility.
Note:This function supports the VARCHAR type.

Syntax

ANYFIRST(string <,start>)

Required Argument

string

is the character constant, variable, or expression to search.

Optional Argument

start

is an optional integer that specifies the position at which the search should start and the direction in which to search.

Details

The ANYFIRST function does not depend on the TRANTAB, ENCODING, or LOCALE system options.
The ANYFIRST function searches a string for the first occurrence of any character that is valid as the first character in a SAS variable name under VALIDVARNAME=V7. These characters are the underscore (_) and uppercase or lowercase English letters. If such a character is found, ANYFIRST returns the position in the string of that character. If no such character is found, ANYFIRST returns a value of 0.
If you use only one argument, ANYFIRST begins the search at the beginning of the string. If you use two arguments, the absolute value of the second argument, start, specifies the position at which to begin the search. The direction in which to search is determined in this way:
  • If the value of start is positive, the search proceeds to the right.
  • If the value of start is negative, the search proceeds to the left.
  • If the value of start is less than the negative length of the string, the search begins at the end of the string.
ANYFIRST returns a value of zero when one of these conditions is true:
  • The character that you are searching for is not found.
  • The value of start is greater than the length of the string.
  • The value of start=0.

Comparisons

The ANYFIRST function searches a string for the first occurrence of any character that is valid as the first character in a SAS variable name under VALIDVARNAME=V7. The NOTFIRST function searches a string for the first occurrence of any character that is not valid as the first character in a SAS variable name under VALIDVARNAME=V7.

Example

This example uses the ANYFIRST function to search a string for any character that is valid as the first character in a SAS variable name under VALIDVARNAME=V7.
data _null_;    
   string='Next = _n_ + 12E3;';  
   j=0;  
   do until(j=0);  
      j=anyfirst(string, j+1);  
      if j=0 then put +3 "That's all";  
      else do;          
         c=substr(string, j, 1);  
         put +3 j= c=;  
      end; 
   end;
run;
SAS writes the following output to the log:
   j=1 c=N
   j=2 c=e
   j=3 c=x
   j=4 c=t
   j=8 c=_
   j=9 c=n
   j=10 c=_
   j=16 c=E
   That's all

See Also

Functions:
Last updated: March 16, 2017