DATA Step, Macro, Functions and more

Difference between %scan and %qscan

Super Contributor
Posts: 625

Difference between %scan and %qscan

May I request someone to illutsrate me the following example to understand the difference between %scan and %qscan?


%macro a;
%mend a;
%macro b;
%mend b;
%macro c;
%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 these lines to the log: 
X: %a*%b*%c
The third word in X, with SCAN: cccccc
The third word in X, with QSCAN: %c

I took this example from



In the example citied above, I would like to understand how does %scan and %qscan function got resolved?


If X resolves to %a*%b*%c then scan function should return %c whereas it returns cccccc. Why so? Whether my underst

Super User
Posts: 10,279

Re: Difference between %scan and %qscan

[ Edited ]

The %scan() function does not prevent the evaluation of the characters & and %. Therefore macro calls and variables are resolved before the scan for words is done.

%qscan prevents macro evaluation, as it treats & and % as "normal" characters.

Therefore you get what the macros resolve to with %scan, and the unresolved macro names with %qscan.

Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super Contributor
Posts: 285

Re: Difference between %scan and %qscan

%qscan is macro quoting function. 'q' is mnemonic for quoting. It treats macro triggers (&, %) as text.
Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation