The %TO value of the %DO I loop is invalid

  • Follow


Hi I have the following test code below. The calls to %add and %leng 
marcos work fine but %addwrong gives me an errors that I don't 
understand how to resolve, any help appreciated:
/*****************************************************/
%macro add (marr, val);
    sum=0;
    %do i=1 %to &val;
        sum= &marr(&i) + sum;
        %end;
    put sum;
    %mend add;

%macro leng(marr);
    len=dim(&marr);
    put len;
    %mend leng;

%macro addwrong (marr);
    sum=0;
    val = dim(&marr);
    %do i=1 %to &val;
        sum= &marr(&i) + sum;
        %end;
    put sum;
    %mend addwrong;

data _null_;
    array arr(7) _temporary_ (1.7 2.7 3 4 7 4 23.8);
    %add(arr,7);
run;

data _null_;
    array arr(7) _temporary_ (1.7 2.7 3 4 7 4 23.8);
    %leng(arr);
run;

data _null_;
    array arr(7) _temporary_ (1.7 2.7 3 4 7 4 23.8);
    %addwrong(arr);
run;
/******************************************************/
The last call gives this error:
ERROR: A character operand was found in the %EVAL function or %IF 
condition where a numeric operand is required. The condition was: &val
ERROR: The %TO value of the %DO I loop is invalid.
ERROR: The macro ADDWRONG will stop executing.

0
Reply franco734 (14) 3/20/2011 11:57:05 AM

On Mar 20, 4:57=A0am, francogrex <fra...@grexdot.org> wrote:
> Hi I have the following test code below. The calls to %add and %leng
> marcos work fine but %addwrong gives me an errors that I don't
> understand how to resolve, any help appreciated:
> /*****************************************************/
> %macro add (marr, val);
> =A0 =A0 sum=3D0;
> =A0 =A0 %do i=3D1 %to &val;
> =A0 =A0 =A0 =A0 sum=3D &marr(&i) + sum;
> =A0 =A0 =A0 =A0 %end;
> =A0 =A0 put sum;
> =A0 =A0 %mend add;
>
> %macro leng(marr);
> =A0 =A0 len=3Ddim(&marr);
> =A0 =A0 put len;
> =A0 =A0 %mend leng;
>
> %macro addwrong (marr);
> =A0 =A0 sum=3D0;
> =A0 =A0 val =3D dim(&marr);
> =A0 =A0 %do i=3D1 %to &val;
> =A0 =A0 =A0 =A0 sum=3D &marr(&i) + sum;
> =A0 =A0 =A0 =A0 %end;
> =A0 =A0 put sum;
> =A0 =A0 %mend addwrong;
>
> data _null_;
> =A0 =A0 array arr(7) _temporary_ (1.7 2.7 3 4 7 4 23.8);
> =A0 =A0 %add(arr,7);
> run;
>
> data _null_;
> =A0 =A0 array arr(7) _temporary_ (1.7 2.7 3 4 7 4 23.8);
> =A0 =A0 %leng(arr);
> run;
>
> data _null_;
> =A0 =A0 array arr(7) _temporary_ (1.7 2.7 3 4 7 4 23.8);
> =A0 =A0 %addwrong(arr);
> run;
> /******************************************************/
> The last call gives this error:
> ERROR: A character operand was found in the %EVAL function or %IF
> condition where a numeric operand is required. The condition was: &val
> ERROR: The %TO value of the %DO I loop is invalid.
> ERROR: The macro ADDWRONG will stop executing.

val isn't a macro variable in that macro.
0
Reply Reeza 3/20/2011 5:48:27 PM


But in general how to you deal with this. Suppose I have a macro:
%macro test (arg=);
array arrayone [&arg];   /* This is ok */
array arraytwo [&arg+1] /* This is not ok */
....
How do we manage these situation when I want %test(3) and then the
arraytwo needs to take dimension 4... ?


0
Reply Francogrex 3/24/2011 8:15:36 PM

you cannot use datastep variable in macro code. val is not a macro
variable, so macro interpreter knows nothing about it.

try to do
%let val =3D %sysfunc(dim(&marr));
instead of
val =3D dim(&marr);

%sysfunc lets you call standard SAS functions and return their results
to _macro_ variables.

On Mar 24, 4:15=A0pm, Francogrex <fra...@grex.org> wrote:
> But in general how to you deal with this. Suppose I have a macro:
> %macro test (arg=3D);
> array arrayone [&arg]; =A0 /* This is ok */
> array arraytwo [&arg+1] /* This is not ok */
> ...
> How do we manage these situation when I want %test(3) and then the
> arraytwo needs to take dimension 4... ?
0
Reply m1st 3/25/2011 9:05:55 AM

3 Replies
455 Views

(page loaded in 0.086 seconds)

Similiar Articles:













7/24/2012 12:11:11 PM


Reply: