Usage Note 35208: How to count words, specific characters, and specific substrings within a text string
Beginning with SAS 9.1.3 there are new functions that can be used to count specific characters, specific substrings, and words in a string.
COUNT, COUNTC, and COUNTW were added to the BASE Functions in SAS 9.1.3.
However, COUNTW is not documented in the 9.1.3 online documentation. It is in 9.2
COUNT will count the number of times a specific substring appears within a character string. Caution: If two occurrences of the specified substring overlap in the string, inconsistent results will be returned.
For example, COUNT('boobooboo', 'booboo') might return a 1 or a 2.
COUNTC will count the number of times a specific character appears within a character string.
COUNTW will count the number of words that appear within a character string.
There are a few samples in the Full Code tab of this note.
In order to better understand the functions and the modifier options available, go to the following link in the SAS 9.2 documentation:
Functions and CALL Routines by CategoryOperating System and Release Information
| SAS System | Base SAS | z/OS | 9.1 TS1M0 | |
| Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.1 TS1M0 | |
| Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.1 TS1M0 | |
| Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.1 TS1M0 | |
| Microsoft Windows 2000 Advanced Server | 9.1 TS1M0 | |
| Microsoft Windows 2000 Datacenter Server | 9.1 TS1M0 | |
| Microsoft Windows 2000 Server | 9.1 TS1M0 | |
| Microsoft Windows 2000 Professional | 9.1 TS1M0 | |
| Microsoft Windows NT Workstation | 9.1 TS1M0 | |
| Microsoft Windows Server 2003 Datacenter Edition | 9.1 TS1M0 | |
| Microsoft Windows Server 2003 Enterprise Edition | 9.1 TS1M0 | |
| Microsoft Windows Server 2003 Standard Edition | 9.1 TS1M0 | |
| Microsoft Windows XP Professional | 9.1 TS1M0 | |
| 64-bit Enabled AIX | 9.1 TS1M0 | |
| 64-bit Enabled HP-UX | 9.1 TS1M0 | |
| 64-bit Enabled Solaris | 9.1 TS1M0 | |
| HP-UX IPF | 9.1 TS1M0 | |
| Linux | 9.1 TS1M0 | |
| OpenVMS Alpha | 9.1 TS1M0 | |
| Tru64 UNIX | 9.1 TS1M0 | |
*
For software releases that are not yet generally available, the Fixed
Release is the software release in which the problem is planned to be
fixed.
Here are a few samples for the functions.
COUNT
/* count 'this' in the string and it is case sensitive */
/* this code will output a 3 to the log */
data one;
xyz='This is a thistle? Yes, this is a thistle.';
howmanythis=count(xyz,'this');
put howmanythis;
run;
/* count 'this' in the string and ignore the case */
/* this code will output a 4 to the log */
data one;
xyz='This is a thistle? Yes, this is a thistle.';
howmanythis=count(xyz,'this','i');
put howmanythis;
run;
COUNTC
/* count lower case 'b' */
/* the code above will output a 1 to the log */
data one;
xyz='Baboons Eat Bananas ';
howmanyb=countc(xyz,'b');
put howmanyb;
run;
COUNTW
/* this code will output a 6 to the log */
data one;
x='a string of words to count';
howmany=countw(x);
put howmany;
run;
| Date Modified: | 2009-03-20 11:01:52 |
| Date Created: | 2009-03-19 14:12:19 |