Return to Archive

SAS Tip of the Month
August 2006

Sometimes we need to know if a character variable contains a number, character, or both, or punctuation -- granted, the latter is rare, but it is sometimes still needed. There are a few functions that can help us with this. These are:

ANYALNUM -- Searches a character string for an alphanumeric character
ANYALPHA -- Searches a character string for an alphabetic character
ANYDIGIT -- Searches a character string for a digit
ANYPUNCT -- Searches a character string for a punctuation

In all cases it finds the first character that meets the condition.

The following code demonstrates its use:

   198 data _null_;
   199 TextStr='1313 Mockingbird Lane, Mockingbird Heights';
   200 FirstALNUM=ANYALNUM(TextStr);
   201 FirstALPHA=ANYALPHA(TextStr);
   202 FirstDIGIT=ANYDIGIT(TextStr);
   203 FirstPUNCT=ANYPUNCT(TextStr);
   204 put TextStr= /
   205 FirstALNUM= FirstALPHA= FirstDIGIT= FirstPUNCT=;
   206 run;

   TextStr=1313 Mockingbird Lane, Mockingbird Heights
   FirstALNUM=1 FirstALPHA=6 FirstDIGIT=1 FirstPUNCT=22
________________________________
Updated August 1, 2006