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.
BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

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.
BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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