Counts the number of times that a specific substring of characters appears within a character string that you specify
Category: Character
COUNT ( string,substring < ,modifiers >)
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 that evaluates to one or more constants.
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, inconsistent results will be returned. For example, COUNT('boobooboo', 'booboo') might return either a 1 or a 2.
The COUNT function counts substrings of characters in a character string, whereas the COUNTC function counts individual characters in a character string.
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 |
Functions:
'COUNTC Function' on page 470
'FIND Function' on page 533
'INDEX Function' on page 589
'RXMATCH Function' on page 799