Explanation of your error as the proper fix was covered by Quentin As with data step DO loops, the %DO also ends by iterating one beyond the end of loop criterion. So at the end of the %do loop &K is effectively 5. You can create a dataset with a do loop and not drop the counter variable and see for yourself. So the issue with your code is that the macro first writes the following code during macro compilation phase and then executes this datastep code: data wilcox1; wil_stat1=resolve('&&wilstat&k'); ... p_val4=resolve('&&pval&k'); output; run; That is, the resolve function resolves macro variable at data step execution phase rather than macro compilation phase as it was within your %sysfunc. So timeliness wise, since the data step is executed after the macro code has been resolved, your code effectively turns into this conceptual hardcoded datastep with only macro variables: data wilcox1; wil_stat1=&wilstat5; p_val1=&pval5; wil_stat1=&wilstat5; p_val1=&pval5; wil_stat1=&wilstat5; p_val1=&pval5; wil_stat1=&wilstat5; p_val1=&pval5; output; run; So the error lies in using the resolve function and the corrections were covered by Quentin and Reeza.
... View more