DATA Step, Macro, Functions and more

Problem with a macro

Reply
Contributor
Posts: 21

Problem with a macro

Hi, I made a program which put the x and y coordonates when a condition is verified but I have an error generated in SAS,

here some informations :

 

%macro mandelbrot(x_min,x_max,y_min,y_max,pas,seuil,n_max);
%let pas_coef=&pas.*100;
%let x_min_coef=(&x_min.*100);
%let x_max_coef=(&x_max.*100);
%let y_min_coef=(&y_min.*100);
%let y_max_coef=(&y_max.*100);
%let u=0;
%let v=0;
         %do i=&x_min_coef. %to &x_max_coef. by &pas_coef.;
             %do j=&y_min_coef. %to &y_max_coef. by &pas_coef.;
                  %let n=0;
                  %let u=0;
                  %let v=0;
                  %let &norm=0;
                  %let x_r=%sysevalf(&i./100);
                  %let y_r=%sysevalf(&j./100);
                  %do %while(&norm.<&seuil. and &n.<&nmax.);
                       %let n=%eval(1+&n.);
                       %let u=%sysevalf((&u.)**2-(&v.)**2+&x_r.);
                       %let v=%sysevalf(2*(&u.)*(&v.)+&y_r.);
                       %let &norm=%module_2(&u.,&v.);
				  %end;
                  
       	 %if (&norm.<&seuil.) %then %do;
			proc sql;
			insert into MANDELBROT values (&x_r.,&y_r.);
			quit;
	    %end;
	   %end;
%end;
%mend mandelbrot;
%mandelbrot(-2,0.5,-1.1,1.1,0.02,2,30);

The error :

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition
       was: &x_max_coef. by &pas_coef.
ERROR: The %TO value of the %DO I loop is invalid.
ERROR: The macro MANDELBROT will stop executing.

Super User
Posts: 2,061

Re: Problem with a macro

should be %by instead of by coz you are using macro language needing macro syntax for the macro processor to recognize

 %do i=&x_min_coef. %to &x_max_coef. %by &pas_coef.;
             %do j=&y_min_coef. %to &y_max_coef. %by &pas_coef.;



Super User
Posts: 24,010

Re: Problem with a macro

A %DO loop likely wants a %BY rather than BY.

 


Num19 wrote:

Hi, I made a program which put the x and y coordonates when a condition is verified but I have an error generated in SAS,

here some informations :

 

%macro mandelbrot(x_min,x_max,y_min,y_max,pas,seuil,n_max);
%let pas_coef=&pas.*100;
%let x_min_coef=(&x_min.*100);
%let x_max_coef=(&x_max.*100);
%let y_min_coef=(&y_min.*100);
%let y_max_coef=(&y_max.*100);
%let u=0;
%let v=0;
         %do i=&x_min_coef. %to &x_max_coef. by &pas_coef.;
             %do j=&y_min_coef. %to &y_max_coef. by &pas_coef.;
                  %let n=0;
                  %let u=0;
                  %let v=0;
                  %let &norm=0;
                  %let x_r=%sysevalf(&i./100);
                  %let y_r=%sysevalf(&j./100);
                  %do %while(&norm.<&seuil. and &n.<&nmax.);
                       %let n=%eval(1+&n.);
                       %let u=%sysevalf((&u.)**2-(&v.)**2+&x_r.);
                       %let v=%sysevalf(2*(&u.)*(&v.)+&y_r.);
                       %let &norm=%module_2(&u.,&v.);
				  %end;
                  
       	 %if (&norm.<&seuil.) %then %do;
			proc sql;
			insert into MANDELBROT values (&x_r.,&y_r.);
			quit;
	    %end;
	   %end;
%end;
%mend mandelbrot;
%mandelbrot(-2,0.5,-1.1,1.1,0.02,2,30);

The error :

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition
       was: &x_max_coef. by &pas_coef.
ERROR: The %TO value of the %DO I loop is invalid.
ERROR: The macro MANDELBROT will stop executing.


 

Contributor
Posts: 21

Re: Problem with a macro

Oh, sorry for that mistake.

Now I have :

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition
       was: &x_max_coef.
ERROR: The %TO value of the %DO I loop is invalid.
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition
       was: &pas_coef.
ERROR: The %BY value of the %DO I loop is invalid.
ERROR: The macro MANDELBROT will stop executing.

Super User
Posts: 2,061

Re: Problem with a macro

[ Edited ]

sorry my bad that i didn't notice at the top, you need %eval in your %let assignments

or to be safe %sysevalf

 

%let pas_coef=&pas.*100;
%let x_min_coef=(&x_min.*100);
%let x_max_coef=(&x_max.*100);
%let y_min_coef=(&y_min.*100);
%let y_max_coef=(&y_max.*100);

Super User
Posts: 13,941

Re: Problem with a macro

Posted in reply to novinosrin

novinosrin wrote:

sorry my bad that i didn't notice at the top, you need %eval in your %let assignments

or to be safe %sysevalf

 

%let pas_coef=&pas.*100;
%let x_min_coef=(&x_min.*100);
%let x_max_coef=(&x_max.*100);
%let y_min_coef=(&y_min.*100);
%let y_max_coef=(&y_max.*100);


AND

%eval only does integers.

See

2740  %let junk= %eval(0.5 *100);
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric
       operand is required. The condition was: 0.5 *100

Yes you would need:

 

%let junk= %sysevalf(0.5 *100);

 

HOWEVER since you are using those values in %do loops ALL the values must be integers on the %do statements. The macro language doesn't work with %do x= .5 %to 10 %by .5;

 

Of course the question is why are you doing data processing in the macro language to begin with???

Especially to do an INSERT into a set using Proc SQL? I don't know what your %module_2 macro does but I'm willing to be this can all be turned into a data step relatively easily.

SAS Super FREQ
Posts: 508

Re: Problem with a macro

I completely agree with @ballardw .  You are making this an order of magnitude harder than it needs to be by using the macro language instead of a DATA step.

Contributor
Posts: 21

Re: Problem with a macro

Error on error I have this one now :

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition
       was: 0.02*100
ERROR: The macro MANDELBROT will stop executing.
Smiley Sad

Super User
Posts: 2,061

Re: Problem with a macro

Mate!, use %sysevalf  instead of %eval

Super User
Posts: 24,010

Re: Problem with a macro

1. Replace BY with %BY

2. Set parameters using %SYSEVALF()

3. Fix Norm value declaration (not &norm, remove &).  

4. NMAX is undeclared/not calculated???

 

%macro mandelbrot(x_min,x_max,y_min,y_max,pas,seuil,n_max);
%let pas_coef=%sysevalf(&pas.*100);
%let x_min_coef=%sysevalf(&x_min.*100);
%let x_max_coef=%sysevalf(&x_max.*100);
%let y_min_coef=%sysevalf(&y_min.*100);
%let y_max_coef=%sysevalf(&y_max.*100);
%let u=0;
%let v=0;
         %do i=&x_min_coef. %to &x_max_coef. %by &pas_coef.;
             %do j=&y_min_coef. %to &y_max_coef. %by &pas_coef.;
                  %let n=0;
                  %let u=0;
                  %let v=0;
                  %let norm=0;
                  %let x_r=%sysevalf(&i./100);
                  %let y_r=%sysevalf(&j./100);
                  %do %while(&norm.<&seuil. and &n.<&nmax.);
                       %let n=%eval(1+&n.);
                       %let u=%sysevalf((&u.)**2-(&v.)**2+&x_r.);
                       %let v=%sysevalf(2*(&u.)*(&v.)+&y_r.);
                       %let &norm=%module_2(&u.,&v.);
				  %end;
                  
       	 %if (&norm.<&seuil.) %then %do;
			proc sql;
			insert into MANDELBROT values (&x_r.,&y_r.);
			quit;
	    %end;
	   %end;
%end;
%mend mandelbrot;
%mandelbrot(-2,0.5,-1.1,1.1,0.02,2,30);

A little bit closer....it seems like this would be easier/faster in a data step or PROC IML.

 

It says NMAX isn't declared, which is true.

 

 

Super User
Posts: 24,010

Re: Problem with a macro

And n_max in the macro list versus nmax used in the code.....
Contributor
Posts: 21

Re: Problem with a macro

I think that's so close, I have this now :

ERROR: Expecting a variable name after %LET.
ERROR: Symbolic variable name 0 must begin with a letter or underscore.
ERROR: The macro MANDELBROT will stop executing.

Super User
Posts: 2,061

Re: Problem with a macro

problem is this 

  %let norm=0; causing vairable name issue hahahahha
Super User
Posts: 2,061

Re: Problem with a macro

Posted in reply to novinosrin

that is causing error in macro var creation here 

    %let &norm=%module_2(&u.,&v.);

all same names begin with a letter or underscore 

Contributor
Posts: 21

Re: Problem with a macro

No error displayed but I have nothing displayed by the macro

Ask a Question
Discussion stats
  • 22 replies
  • 327 views
  • 3 likes
  • 6 in conversation