BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

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

3 REPLIES 3
LinusH
Tourmaline | Level 20

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
Quentin
Super User

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.
Ksharp
Super User

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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