Chapter Contents

Previous

Next
%SCAN and %QSCAN

%SCAN and %QSCAN



Search for a word that is specified by its position in a string

Type: Macro functions
See also:
%NRBQUOTE
%STR and %NRSTR


Syntax
Details
Comparisons
Example
Comparing the Actions of %SCAN and %QSCAN

Syntax

%SCAN(argument,n<,delimiters>)
%QSCAN(argument,n<,delimiters>)

argument
is a character string or a text expression. If argument might contain a special character or mnemonic operator, listed below, use %QSCAN. If argument contains a comma, enclose argument in a quoting function, for example, %QUOTE(argument).

n
is an integer or a text expression that yields an integer, which specifies the position of the word to return. (An implied %EVAL gives n numeric properties.) If n is greater than the number of words in argument, the functions return a null string.

delimiters
specifies an optional list of one or more characters that separate "words" or text expressions that yield one or more characters. To use a single blank or a single comma as the only delimiter, you must enclose the character in the %STR function, for example %STR( ) or %STR(,). The delimiters recognized by %SCAN and %QSCAN vary between ASCII and EBCDIC systems. If you omit delimiters, SAS treats these characters as default delimiters:

ASCII systems
blank . < ( + & ! $ * ) ; ^ - / , % |

EBCDIC systems
blank . < ( + | & ! $ * ) ; ¬ - / , % | ¢

If delimiters includes any of the default delimiters for your system, the remaining default delimiters are treated as text.

To determine if you are using an ASCII or EBCDIC system, see the SAS companion for your operating system.


Details

The %SCAN and %QSCAN functions search argument and return the nth word. A word is one or more characters separated by one or more delimiters.

%SCAN does not mask special characters or mnemonic operators in its result, even when the argument was previously masked by a macro quoting function. %QSCAN masks the following special characters and mnemonic operators in its result:

& % ' " ( ) + - * / < > = ¬ ^ ~ ; , blank
AND OR NOT EQ NE LE LT GE GT


Comparisons

%QSCAN masks the same characters as the %NRBQUOTE function.


Example

Example 1: Comparing the Actions of %SCAN and %QSCAN

This example illustrates the actions of %SCAN and %QSCAN.

%macro a;
   aaaaaa
%mend a;
%macro b;
   bbbbbb
%mend b;
%macro c;
   cccccc
%mend c;

%let x=%nrstr(%a*%b*%c);
%put X: &x;
%put The third word in X, with SCAN: %scan(&x,3,*);
%put The third word in X, with QSCAN: %qscan(&x,3,*);

The %PUT statement writes this line:

X: %a*%b*%c
The third word in X, with SCAN: cccccc
The third word in X, with QSCAN: %c


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.