BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kstatju
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

9 REPLIES 9
Astounding
PROC Star

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.

 

 

ballardw
Super User

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?

kstatju
Fluorite | Level 6

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.

slangan
Obsidian | Level 7

@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 #

Astounding
PROC Star

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.

ballardw
Super User

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.

Tom
Super User Tom
Super User

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 (%*....; ).

 

Tom
Super User Tom
Super User

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

kstatju
Fluorite | Level 6

Thank you all. Great help!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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