I know %do %until checks the loop exit condition at the bottop of loop while %do %while checks at the top of loop. Can %do %until be replaced by %do %while in all conditions? Are there any situations when only %do %until or %do %while can be used?
%macro m;
%local i;
%let i=0;
%do %until (&i ge 3);
data dat&i;
set sashelp.class;
run;
%let i=%eval(&i+1);
%end;
%mend m;
One situation where %DO %WHILE can work and %DO %UNTIL cannot work is when you have a condition for which you want the inside of the loop to never execute. On the first iteration of the loop if a %DO %WHILE condition is false, the code inside the loop will not be excecuted. But on the first iteration of a %DO %UNTIL loop if the condition is true, the inside of the loop will be executed. Conversely, if you want to force the inside of the loop to ALWAYS execute at least once, then you would want %DO %UNTIL, not %DO WHILE.
As an example, consider the below macros, which print a list of datasets with a %DO %WHILE loop or a %DO %UNTIL loop:
%macro PrintListWhile(datalist=,obs=5);
%local i data;
%let i=1;
%let data=%scan(&datalist,&i,%str( ));
%do %while(%superq(data) ne %str());
proc print data=&data(obs=&obs);
run;
%let i=%eval(&i+1);
%let data=%scan(&datalist,&i,%str( ));
%end;
%mend;
%macro PrintListUntil(datalist=,obs=5);
%local i data;
%let i=1;
%let data=%scan(&datalist,&i,%str( ));
%do %until(%superq(data) eq %str());
proc print data=&data(obs=&obs);
run;
%let i=%eval(&i+1);
%let data=%scan(&datalist,&i,%str( ));
%end;
%mend;
Both macros will give you the same result when you pass it a list of datasets:
%PrintListWhile(datalist=sashelp.class sashelp.shoes sashelp.prdsale) %PrintListUntil(datalist=sashelp.class sashelp.shoes sashelp.prdsale)
But if you pass it an empty list:
%PrintListWhile(datalist=) %PrintListUntil(datalist=)
%PrintListWhile will work. It will not generate any PROC PRINT code.
%PrintListUntil will error, because it generates an invalid PROC PRINT step:
231 %PrintListUntil(datalist=) NOTE: Line generated by the invoked macro "PRINTLISTUNTIL". 1 proc print data=&data(obs=&obs); run; - 22 200 MPRINT(PRINTLISTUNTIL): proc print data=(obs=5) NAME; ERROR: File WORK.NAME.DATA does not exist. MPRINT(PRINTLISTUNTIL): run; ERROR 22-322: Expecting a name. ERROR 200-322: The symbol is not recognized and will be ignored.
I'm not sure, but probably. But either one can be awkward in different situations - that's why you got both to chose from.
You'ra asking for an absolute answer. Why? Do you have a specific problem?
One situation where %DO %WHILE can work and %DO %UNTIL cannot work is when you have a condition for which you want the inside of the loop to never execute. On the first iteration of the loop if a %DO %WHILE condition is false, the code inside the loop will not be excecuted. But on the first iteration of a %DO %UNTIL loop if the condition is true, the inside of the loop will be executed. Conversely, if you want to force the inside of the loop to ALWAYS execute at least once, then you would want %DO %UNTIL, not %DO WHILE.
As an example, consider the below macros, which print a list of datasets with a %DO %WHILE loop or a %DO %UNTIL loop:
%macro PrintListWhile(datalist=,obs=5);
%local i data;
%let i=1;
%let data=%scan(&datalist,&i,%str( ));
%do %while(%superq(data) ne %str());
proc print data=&data(obs=&obs);
run;
%let i=%eval(&i+1);
%let data=%scan(&datalist,&i,%str( ));
%end;
%mend;
%macro PrintListUntil(datalist=,obs=5);
%local i data;
%let i=1;
%let data=%scan(&datalist,&i,%str( ));
%do %until(%superq(data) eq %str());
proc print data=&data(obs=&obs);
run;
%let i=%eval(&i+1);
%let data=%scan(&datalist,&i,%str( ));
%end;
%mend;
Both macros will give you the same result when you pass it a list of datasets:
%PrintListWhile(datalist=sashelp.class sashelp.shoes sashelp.prdsale) %PrintListUntil(datalist=sashelp.class sashelp.shoes sashelp.prdsale)
But if you pass it an empty list:
%PrintListWhile(datalist=) %PrintListUntil(datalist=)
%PrintListWhile will work. It will not generate any PROC PRINT code.
%PrintListUntil will error, because it generates an invalid PROC PRINT step:
231 %PrintListUntil(datalist=) NOTE: Line generated by the invoked macro "PRINTLISTUNTIL". 1 proc print data=&data(obs=&obs); run; - 22 200 MPRINT(PRINTLISTUNTIL): proc print data=(obs=5) NAME; ERROR: File WORK.NAME.DATA does not exist. MPRINT(PRINTLISTUNTIL): run; ERROR 22-322: Expecting a name. ERROR 200-322: The symbol is not recognized and will be ignored.
"Can %do %until be replaced by %do %while in all conditions?"
No. but %do %while could replace %do %until.
There is very important different thing between them is %do %until will iterative at least once , but %do %while could iterative ZERO time.
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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: