From: Tony on
Hi All,

I had the following codes and there were running errors. The macro
variables GRP1 and GRP2 were correctly resolved. However, I got this
error 200 from the proc sql statement. After I change the %qscan to
%scan, it worked! I thought I should use %qscan to mask the "-" in the
macro variable OABYGROUP. How come it only works with %scan?

Thanks in advance.
Tony

Error Message:

1 proc sql; select count(*) from a where &GRP1; quit;
-
200
ERROR 200-322: The symbol is not recognized and will be ignored.


SAS Codes:

options nocenter formdlim=' ' nodate nonumber symbolgen mlogic;
options obs=0;

%let OABYGROUP=%str(CAT="ACTIVES-SSG" CAT="ACTIVES-DC");
data a;
CAT="ACTIVES-SSG";
mem_no="ABCDEFG";
run;

%macro a;
%local g;
%let g=1;
%do %while(%length(%qscan(&OAbygroup,&g,%str( ))) GT 0);
%let GRP&g=%qscan(&OAbygroup,&g,%str( ));
%put &&GRP&g;
%let g=%eval(&g + 1);
%end;
%let g=%eval(&g -1);
%put &GRP1;
%put &GRP2;
proc sql;
select count(*) from a where &GRP1;;
quit;
%mend;
%a;
From: Ian Whitlock on
Summary: Short lesson in macro quoting
#iw-value=2

Tony <tonywof(a)GMAIL.COM> wanted to know what is wrong with the code:

options nocenter formdlim=' ' nodate nonumber symbolgen mlogic;
options obs=0;


%let OABYGROUP=%str(CAT="ACTIVES-SSG" CAT="ACTIVES-DC");
data a;
CAT="ACTIVES-SSG";
mem_no="ABCDEFG";
run;


%macro a;
%local g;
%let g=1;
%do %while(%length(%qscan(&OAbygroup,&g,%str( ))) GT 0);
%let GRP&g=%qscan(&OAbygroup,&g,%str( ));
%put &&GRP&g;
%let g=%eval(&g + 1);
%end;
%let g=%eval(&g -1);
%put &GRP1;
%put &GRP2;
proc sql;
select count(*) from a where &GRP1;;
quit;
%mend;
%a;

and why did changing %QSCAN to %SCAN make the code work. Well the
change meant that something was no longer quoted. So perhaps a better
question is what needed unquoting. Well what was the WHERE clause?

where CAT="ACTIVES-SSG"

It should be apparent that that =-sign should not be quoted. You
could check by simply changing to

where %unquote(&GRP1)

This fixes the symptom - an nonworking program. Now we must turn to
the deeper question - why was quoting used in the first place, or
better - what needs quoting in the original line

%let OABYGROUP=%str(CAT="ACTIVES-SSG" CAT="ACTIVES-DC");

The minus sign might need quoting, but it is already enclosed in
double quotes, so that cannot be the problem. The double quotes might
need hiding, but they occur in pairs, so they won't cause problems.
the only thing left is the =-sign. Where does that cause problems?
Anywhere %EVAL is used. However none of the code uses either an
explicit %EVAL or even an implied %EVAL.

The %DO %WHILE would cause problems had it been written

%do %while(%scan(&OAbygroup,&g,%str( )) ^= %str());

since the expression

CAT="ACTIVES-SSG" ^= %str()

would be sent to %EVAL and that first =-sign should not mean equal.
This one of the best arguments for using %LENGTH in such tests - it
eliminates the need for quoting a lot of the time.

We now see that changing %QSCAN to %SCAN accidentally fixed a quoting
problem because %SCAN returns an unquoted value and %QSCAN does not.
However, the real problem was that there never should have been any
quoting in the first place. Had there been a need for it, then
changing %QSCAN to %SCAN would not have worked some place.

Now while we are focused on the code, lets take a closer look at

where &GRP1

This is kind of hard to read because the reader has no idea what is
going on at this point without referring far away to the initial %LET.
From a local reading point of view it would be better to have

where &var = "&val"

I would have achieved this with

%macro a ( var=cat , values=ACTIVES-SSG ACTIVES-DC ) ;


and the expression

where &var = "&grp1"

The affect on readability of how one assigns macro variable values is
discussed in more detail in "Macro Design Issues"
www2.sas.com/proceedings/sugi27/p067-27.pdf A deeper understanding of
macro quoting can be found in "A Serious Look at Macro Quoting"
www2.sas.com/proceedings/sugi28/011-28.pdf In general use
http://lexjansen.com/sugi/ to locate many other papers that can help
you with SAS.

--
Ian Whitlock
From: Tony on
This really helps a lot. I appreciate your short lessons and all your
suggestions. Thanks, Ian.

Tony

On Dec 25, 5:17 pm, iw1...(a)GMAIL.COM (Ian Whitlock) wrote:
> Summary: Short lesson in macro quoting
> #iw-value=2
>>
> The affect on readability of how one assigns macro variable values is
> discussed in more detail in "Macro Design Issues"
> www2.sas.com/proceedings/sugi27/p067-27.pdf  A deeper understanding of
> macro quoting can be found in "A Serious Look at Macro Quoting"
> www2.sas.com/proceedings/sugi28/011-28.pdf In general usehttp://lexjansen..com/sugi/to locate many other papers that can help
> you with SAS.
>
> --
> Ian Whitlock