COUNTC Function

Counts the number of characters in a string that appear or do not appear in a list of characters.

Category: Character
Restriction: I18N Level 1 functions should be avoided, if possible, if you are using a non-English language. The I18N Level 1 functions might not work correctly with Double Byte Character Set (DBCS) or Multi-Byte Character Set (MBCS) encodings under certain circumstances.

Syntax

COUNTC(string, charlist <,modifiers> )

Required Arguments

string

specifies a character constant, variable, or expression in which characters are counted.

Tip Enclose a literal string of characters in quotation marks.

charlist

specifies a character constant, variable, or expression that initializes a list of characters. COUNTC counts characters in this list, provided that you do not specify the V modifier in the modifier argument. If you specify the V modifier, then all characters that are not in this list are counted. You can add more characters to the list by using other modifiers.

Tips Enclose a literal string of characters in quotation marks.
If there are no characters in the list after processing the modifiers, COUNTC returns 0.

Optional Argument

modifier

specifies a character constant, variable, or expression in which each non-blank character modifies the action of the COUNTC function. Blanks are ignored. The following characters, in uppercase or lowercase, can be used as modifiers:

blank is ignored.
a or A adds alphabetic characters to the list of characters.
b or B scans string from right to left, instead of from left to right.
c or C adds control characters to the list of characters.
d or D adds digits to the list of characters.
f or F adds an underscore and English letters ( that is, the characters that can begin a SAS variable name using VALIDVARNAME=V7) to the list of characters.
g or G adds graphic characters to the list of characters.
h or H adds a horizontal tab to the list of characters.
i or I ignores case.
l or L adds lowercase letters to the list of characters.
n or N adds digits, an underscore, and English letters (that is, the characters that can appear in a SAS variable name using VALIDVARNAME=V7) to the list of characters.
o or O processes the charlist and modifier arguments only once, at the first call to this instance of COUNTC. If you change the value of charlist or modifier in subsequent calls, the change might be ignored by COUNTC.
p or P adds punctuation marks to the list of characters.
s or S adds space characters to the list of characters (blank, horizontal tab, vertical tab, carriage return, line feed, and form feed).
t or T trims trailing blanks from string and chars. If you want to remove trailing blanks from only one character argument instead of both (or all) character arguments, use the TRIM function instead of the COUNTC function with the T modifier.
u or U adds uppercase letters to the list of characters.
v or V counts characters that do not appear in the list of characters. If you do not specify this modifier, then COUNTC counts characters that do appear in the list of characters.
w or W adds printable characters to the list of characters.
x or X adds hexadecimal characters to the list of characters.
Tip If modifier is a constant, enclose it in quotation marks. Specify multiple constants in a single set of quotation marks.

Details

The COUNTC function allows character arguments to be null. Null arguments are treated as character strings with a length of zero. If there are no characters in the list of characters to be counted, COUNTC returns zero.

Comparisons

The COUNTC function counts individual characters in a character string, whereas the COUNT function counts substrings of characters in a character string.

Example

The following example uses the COUNTC function with and without modifiers to count the number of characters in a string.
data test;
   string  = 'Baboons Eat Bananas     ';
   a       = countc(string, 'a');
   b       = countc(string,'b');
   b_i     = countc(string,'b','i');
   abc_i   = countc(string,'abc','i');
      /* Scan string for characters that are not "a", "b", */
      /* and "c", ignore case, (and include blanks).       */
   abc_iv  = countc(string,'abc','iv');
      /* Scan string for characters that are not "a", "b", */
      /* and "c", ignore case, and trim trailing blanks.   */
   abc_ivt = countc(string,'abc','ivt');
run;

options pageno=1 ls=80 nodate;
proc print data=test noobs;
run;
Output from Using the COUNTC Functions with and without Modifiers
Output from Using the COUNTC Functions with and without Modifiers