DATA Step, Macro, Functions and more

%do %until vs %do %while

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

%do %until vs %do %while

[ Edited ]

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;

 

 


Accepted Solutions
Solution
‎02-28-2016 11:06 PM
PROC Star
Posts: 1,322

Re: %do %until vs %do %while

Posted in reply to SAS_inquisitive

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.

View solution in original post


All Replies
Super User
Posts: 5,424

Re: %do %until vs %do %while

Posted in reply to SAS_inquisitive

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?

Data never sleeps
Solution
‎02-28-2016 11:06 PM
PROC Star
Posts: 1,322

Re: %do %until vs %do %while

Posted in reply to SAS_inquisitive

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.
Super User
Posts: 10,018

Re: %do %until vs %do %while

Posted in reply to SAS_inquisitive

"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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 1703 views
  • 2 likes
  • 4 in conversation