Sample 24737: Search a character expression for a string, specific character, or word
Choose appropriate INDEX function to find target strings,
individual letters, or strings on word boundaries.
Note:
Sample 1 uses INDEX to search for the first
occurrence of a 'word' anywhere in a string. If the
string is not found, the result is zero.
Sample 2 uses INDEXC to locate the first occurence of
any character specified in the excerpt. If no target
is found, the result is zero.**
Sample 3 uses INDEXW to find the target excerpt in a
string on a word boundary. If the word is not found, the
result is zero.
**For SAS 9.0 or above, see the ANY family and NOT family
of functions for more string-searching flexibility.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* Sample 1 : INDEX */
data one;
input string $25.;
/* Search for the word 'cat' */
position=index(string,'cat');
/* Search for the letter 'c' */
letter=INDEX(string,'c');
datalines;
the cat came back
catastrophic
curious cat caterwauls
;
proc print data=one;
run;
/* Sample 2: INDEXC */
data two;
input string $25.;
if indexc(string,'0123456789')> 0 then has_numbers=string;
else no_numbers=string;
datalines;
Box 101
Pine Street
;
proc print data=two;
run;
/* Sample 3: INDEXW */
data three;
input string $25.;
if indexw(string,'my') > 0 then contains_the_word_my='yes';
datalines;
my aunt amy
in the army
my oh my
;
proc print data=three;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Sample 1: INDEX
Obs string position letter
1 the cat came back 5 5
2 catastrophic 1 1
3 curious cat caterwauls 9 1
Sample 2: INDEXC
has_
Obs string numbers no_numbers
1 Box 101 Box 101
2 Pine Street Pine Street
Sample 3: INDEXW
contains_
the_word_
Obs string my
1 my aunt amy yes
2 in the army
3 my oh my yes
Choose appropriate INDEX function to find target strings,
individual letters, or strings on word boundaries.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Functions ==> Character
|
| Date Modified: | 2005-12-16 03:02:58 |
| Date Created: | 2004-09-30 14:09:09 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |