Hello,
Could anyone let me know why my Do loop code didn't work? Thanks.
%let Y1=sum;
%let Y2=los;
%let X1=age;
%let X2=Preterm;
%let X3=ICU;
%macro aaa;
proc npar1way WILCOXON data=Pre_Cal;
%do z = 1 %to 2;
%do i = 1 %to 3;
class &&X&i..;
var &&Y&z..;
%end;
%end; ;
run;
%mend;
%aaa;
You have extra dots in the CLASS and VAR statements. Get rid of them (at least get rid of one per statement).
No, on second thought that's not it.
Have you considered posting the log?
I run the codes, then nothing happed. Log window message was shown.
That's really it??? No further messages after that in the log???
Them something else must have gone wrong in the earlier code ... a missing semicolon, a missing %mend statement, ... something that isn't presented here. I would suggest starting a new SAS session and re-running the code.
What SAS code is it that you are trying to generate?
I do not use that PROC but the syntax you are trying to generate looks really fishy to me.
What would this code even mean?
proc npar1way WILCOXON data=Pre_Cal;
class age;
var sum;
class preterm;
var sum;
class ICU;
var sum;
class age;
var los;
class preterm;
var los;
class ICU;
var los;
run;
Normally you just have one VAR statement and one CLASS statement in a proc. You could use multiple class statements if for some reason you needed to use differing options for some of the class variables.
@Tom is correct, your loop is in the wrong location
See code below for the fix and example that works.
%let Y1=sum;
%let Y2=los;
%let X1=age;
%let X2=Preterm;
%let X3=ICU;
%macro aaa;
%do z = 1 %to 2;
%do i = 1 %to 3;
proc npar1way WILCOXON data=Pre_Cal;
class &&X&i..;
var &&Y&z..;
run;
%end;
%end; ;
%mend;
options mprint symbolgen;
%aaa;
%let Y1=ageatstart;
%let Y2=height;
%let X1=weight_status;
%let X2=deathcause;
%let X3=smoking_status;
%macro aaa;
%do z = 1 %to 2;
%do i = 1 %to 3;
proc npar1way WILCOXON data=sashelp.heart;
class &&X&i..;
var &&Y&z..;
run;
%end;
%end; ;
%mend;
options mprint symbolgen;
%aaa;
Looks like you only need to the %DO loop over the CLASS variables. You can only have one CLASS variable but you can have many variables on the VAR statement.
You also only need TWO macro variables. One to list the CLASS variables and one to list the analysis variables.
Making a "series" of macro variables is just adding extra work for no real advantage.
%let classes=age preterm icu;
%let varlist=sum los;
%do i=1 to %sysfunc(countw(&classes,%str( )));
proc npar1way WILCOXON data=Pre_Cal;
class %scan(&classes,&i,%str( ));
var &varlist;
run;
%end;
You might indicate what code you are attempting to generate.
Looks like a very cumbersome approach without more details.
One suspects that you have created an unstable situation with one or more attempts to execute the code and it is time to restart SAS if the Log isn't actually showing the MPRINT output.
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.