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.
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.
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.
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?
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.
@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 #
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.
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.
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 (%*....; ).
I do not see what the macro logic is adding to this data step. Why not just use a normal data step DO loop?
Thank you all. Great help!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.