Sample 25025: Search a text string for a 'pattern' multiple times in succession
Parse a text string for the letters 'b' or 'c' or 'r' followed by 'at'. Locate all possible occurrences, not just the first.
Note:
The CALL PRXNEXT routine searches a text string for a pattern. It returns the position and length of a pattern match that is located between the start and the stop positions in the string. Because the value of the start parameter is updated to be the position of the next character that follows a match, CALL PRXNEXT enables you to search a string for a pattern multiple times in succession.
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.
/* Find all instances of cat, rat, or bat within TEXT */
data _null_;
patternID = prxparse('/[crb]at/');
text = 'The woods have a bat, cat, and a rat!';
start = 1;
stop = length(text);
/* Use PRXNEXT to find the first instance of the pattern, */
/* then use DO WHILE to find all further instances. */
/* PRXNEXT changes the start parameter so that searching */
/* begins again after the last match. */
call prxnext(patternID, start, stop, text, position, length);
do while (position > 0);
found = substr(text, position, length);
put found= position= length=;
call prxnext(patternID, start, stop, text, position, length);
end;
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.
OUTPUT to SAS log
found=bat position=18 length=3
found=cat position=23 length=3
found=rat position=34 length=3
Use CALL PRXNEXT to search a string for a specified pattern and for multiple occurrences.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> CALL routines
|
| Date Modified: | 2005-12-08 11:34:39 |
| Date Created: | 2005-01-13 15:09:55 |
Operating System and Release Information
| SAS System | Base SAS | z/OS | 9 TS M0 | n/a |
| Microsoft® Windows® for 64-Bit Itanium-based Systems | 9 TS M0 | n/a |
| 64-bit Enabled HP-UX | 9 TS M0 | n/a |
| 64-bit Enabled AIX | 9 TS M0 | n/a |
| 64-bit Enabled Solaris | 9 TS M0 | n/a |
| HP-UX IPF | 9 TS M0 | n/a |
| OpenVMS Alpha | 9 TS M0 | n/a |
| Linux | 9 TS M0 | n/a |
| Tru64 UNIX | 9 TS M0 | n/a |