|
This seems somewhat similar to the first question I asked on SAS L :)
In your first piece of code
%let c = %b
SAS executes the macro %b first and returns what is "left over" from
the %b macro, which is the values of &i. I see this as more of a SAS
trick rather than what the macro language was intended to do -
although there are some old timers who may correct me on this. You
therefore have advertently/inadvertently created a macro function that
returns the values of a function which can then be assigned to a
variable.
In the second part of the code however, you are trying again to take
the output of a loop and assign it to a macro variable. However this
time it is recognised as an assignment and the "%do i=1 %to &m" is
seen as text, and as there are no macro quoting functions used and SAS
recognises this is macro syntax it issues the error message.
I think this raises another interesting question, can SAS macro create
macro code that is executable at a later time? As far as I know, Lisp
allows macro code to create macro code ad infinitum. I don't think SAS
does allow this, but I would be happy to be proven wrong on this. An
example:
%let c = %superq(%put 1;);
%unquote(c);
2009/1/16 he.terry@gmail.com <he.terry@gmail.com>:
> I have two programs. One is right and the other is wrong. I am not
> sure about the reason.
>
> The right code is as follow:
>
> options symbolgen mprint;
> %let m=2;
> %macro b;
> %do i = 1 %to &m;
> &i
> %end;
> %mend b;
>
> %let c = %b
> ;
>
>
> %macro a;
> %put &c;
> %mend a;
> %a;
>
> The wrong code is as follow:
> options symbolgen mprint;
> %let m=2;
> %let c = %do i = 1 %to &m;
> &i
> %end;
> ;
>
>
> %macro a;
> %put &c;
> %mend a;
> %a;
>
|