Previous Page | Next Page

Functions and CALL Routines

COUNT Function



Counts the number of times that a specified substring appears within a character string.
Category: Character
Restriction: I18N Level 1
Tip: You can use the KCOUNT function in SAS National Language Support (NLS): Reference Guide for DBCS processing, but the functionality is different. See DBCS Compatibility.

Syntax
Arguments
Details
The Basics
DBCS Compatibility
Comparisons
Examples
See Also

Syntax

COUNT(string, substring <,modifiers>)


Arguments

string

specifies a character constant, variable, or expression in which substrings are to be counted.

Tip: Enclose a literal string of characters in quotation marks.
substring

is a character constant, variable, or expression that specifies the substring of characters to count in string.

Tip: Enclose a literal string of characters in quotation marks.
modifiers

is a character constant, variable, or expression that specifies one or more modifiers. The following modifiers can be in uppercase or lowercase:

i

ignores character case during the count. If this modifier is not specified, COUNT only counts character substrings with the same case as the characters in substring.

t

trims trailing blanks from string and substring.

Tip: If the modifier is a constant, enclose it in quotation marks. Specify multiple constants in a single set of quotation marks. Modifier can also be expressed as a variable or an expression.

Details


The Basics

The COUNT function searches string, from left to right, for the number of occurrences of the specified substring, and returns that number of occurrences. If the substring is not found in string, COUNT returns a value of 0.

CAUTION:
If two occurrences of the specified substring overlap in the string, the result is undefined.

For example, COUNT('boobooboo', 'booboo') might return either a 1 or a 2.  [cautionend]


DBCS Compatibility

You can use the KCOUNT function, which is documented in SAS National Language Support (NLS): Reference Guide, for DBCS processing, but the functionality is different.

If the value of substring in the COUNT function is longer than two bytes, then the COUNT function can handle DBCS strings. The following examples show the differences in syntax:

COUNT(string, substring <,modifiers>
KCOUNT(string)

Comparisons

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


Examples

The following SAS statements produce these results:

SAS Statements Results
xyz='This is a thistle? Yes, this is a thistle.';
howmanythis=count(xyz,'this');
put howmanythis;

3
xyz='This is a thistle? Yes, this is a thistle.';
howmanyis=count(xyz,'is');
put howmanyis;

6
howmanythis_i=count('This is a thistle? Yes, this is a thistle.'
   ,'this','i');
put howmanythis_i;

4
variable1='This is a thistle? Yes, this is a thistle.';
variable2='is ';
variable3='i';
howmanyis_i=count(variable1,variable2,variable3);
put howmanyis_i;



4
expression1='This is a thistle? '||'Yes, this is a thistle.';
expression2=kscan('This is',2)||'     ';
expression3=compress('i     '||'     t');
howmanyis_it=count(expression1,expression2,expression3);
put howmanyis_it;



6


See Also

Functions:

COUNTC Function

COUNTW Function

FIND Function

INDEX Function

Previous Page | Next Page | Top of Page