DATA Step, Macro, Functions and more

Macro Error

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Macro Error

Hi,

 

I was trying to write some macro in sas. However, it is not working. Here is the code.

 

* %macro product(num, v1, v2, v3, v4, v5, v6, v7, v8);

       %macro product(num, variable1-variable&n);

       %let produc = 0;
       %let variable = 'var';

         %do j = 1 %to #
              %let k=j;
               &produc = &produc + j * v&k;
         %end;
%mend product;

 

%let n = 8;
%let prod = 162;

data zeo_corr.perm&n (keep= var1 - var&n produc)

length default = 3;
array a{&n} var1-var&n;
do j = 1 to 10000;
        do i = 1 to &n;

                 a[i]=j;

        end;

    %product(&n, var1- var&n);

   * %product(&n, var1, var2, var3, var4, var5, var6, var7, var8);
    if produc = &prod then output;

end;

run;

 

it is showing the following error

NOTE: Line generated by the macro variable "PRODUC".
1             0
              -
              180

 

ERROR 180-322: Statement is not valid or it is used out of proper order.

 

or

 

ERROR: More positional parameters found than defined.

 

Where is the problem? Please suggest me the possible solution.


Accepted Solutions
Solution
‎10-30-2015 11:58 AM
Super User
Posts: 5,093

Re: Macro Error

kstatju,

 

I agree with ballardw ... there are too many issues with the code to work out each one individually here.

 

Take the code, reduce it from 8 variables down to 4 variables, and get rid of all the macro language.  Come up with working code using just SAS language, and then we can worry about how to turn your code into a macro.

View solution in original post


All Replies
Super User
Posts: 5,093

Re: Macro Error

This line will cause trouble:

 

&produc = &produc + j * v&k;

 

Since &produc is 0, you are generating a statement that begins with:

 

0 = 0 + ...;

 

0 to the left of an equal sign will not give you a valid SAS statement.  So you need to first determine what SAS statement belongs at that point in the program.

 

 

Super User
Posts: 10,538

Re: Macro Error

It may help to show the program that worke without macros so we can tell what you are actually attempting to accomplish.

 

I see a number of other issues:

You assign a value to a macro variable name Variable but don't use it.

You create a reference to variables V1 V2 etc in the resulting code. Are you sure those variables exist in the input data set?

Occasional Contributor
Posts: 6

Re: Macro Error

New code and its Error.

 

%let n = 8;
%let z = 40;
%let prod = 162;

%macro product(num, var1, var2, var3, var4, var5, var6, var7, var8);
 %let produc = 0;
 %let variable = var;
   %do j = 1 %to num;
  %let k=j;
  sum = %SYSFUNC(sum(&produc + j * %SYSFUNC(cat(&&variable&k))));
  &produc = sum;
 %end;
%mend product;


/** generate all permutations of n elements, in order **/

data zeo_corr.perm&n (keep= var1 - var&n);
length default = 3;
array a{&n} var1-var&n;
do i = 1 to fact(&n);
   do i = 1 to &n;
           a[i]=i;
   end;

    num = &n;
 %product(num, var1, var2, var3, var4, var5, var6, var7, var8);
 if produc = &prod then output;

end;
run;

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric
       operand is required. The condition was: num
ERROR: The %TO value of the %DO J loop is invalid.
ERROR: The macro PRODUCT will stop executing.

Contributor
Posts: 20

Re: Macro Error


kstatju wrote:

New code and its Error.

 


 %let variable = var;
   %do j = 1 %to num;
  %let k=j

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric
       operand is required. The condition was: num
ERROR: The %TO value of the %DO J loop is invalid.
ERROR: The macro PRODUCT will stop executing.


shouldn't it be

   %do j = 1 %to #

Solution
‎10-30-2015 11:58 AM
Super User
Posts: 5,093

Re: Macro Error

kstatju,

 

I agree with ballardw ... there are too many issues with the code to work out each one individually here.

 

Take the code, reduce it from 8 variables down to 4 variables, and get rid of all the macro language.  Come up with working code using just SAS language, and then we can worry about how to turn your code into a macro.

Super User
Posts: 10,538

Re: Macro Error

And learn to use the system options MPRINT and SYMBOLGEN to see what your code is generating. The errors will appear in the log closer to the actual cause instead of all at the end of the macro.

Super User
Super User
Posts: 6,502

Re: Macro Error

[ Edited ]

Do NOT use statement style comments (*....; ) to attempt to comment out macro code.  The macro code inside of these statements will continue to be evaluated the same as they would inside of any other statement. Either use blocks comments (/*....*/) or macro comments (%*....; ).

 

Super User
Super User
Posts: 6,502

Re: Macro Error

I do not see what the macro logic is adding to this data step.  Why not just use a normal data step DO loop?

Occasional Contributor
Posts: 6

Re: Macro Error

Thank you all. Great help!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 574 views
  • 7 likes
  • 5 in conversation