DATA Step, Macro, Functions and more

%GOTO vs CONTINUE in regular DO LOOP

Accepted Solution Solved
Reply
Regular Contributor
Posts: 234
Accepted Solution

%GOTO vs CONTINUE in regular DO LOOP

I have always been confused with the use of GOTO and %GOTO. Does this code simulate the use of CONTINUE in DO LOOP?

%macro m;
  %do i =1 %to 3;
   %if &i=2 %then %goto x;
    data dat&i;
	  set sashelp.class;
	run;
  %x: %end;
%mend m;
%m

Accepted Solutions
Solution
‎02-28-2016 01:40 PM
Respected Advisor
Posts: 3,777

Re: %GOTO vs CONTINUE in regular DO LOOP

Yes your %GOTO does simulate the nonexistent %CONTINUE. 

 

GOTO is the most basic branching construct. CONTINUE and LEAVE statements are just GOTO in with implied labels.

 

Before there was DO in all its forms there was GOTO.  All DO forms can be coded with GOTO, at the heart of those structures the much maligned GOTO is toiling away unseen.

 

GOTO doesn't make programs bad, programmers make programs bad.

 

 

View solution in original post


All Replies
Super User
Posts: 6,927

Re: %GOTO vs CONTINUE in regular DO LOOP

GOTO in any form complicates programs and makes them more error prone. In almost 20 years of SAS programming I have never used it, neither GOTO nor %GOTO

In your case a simple change of the condition to

%if &i ne 2 %then %do;

and insertion of a corresponding %end will do the trick. And this works basically everywhere a goto is (mis)used.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 177

Re: %GOTO vs CONTINUE in regular DO LOOP

You can eliminate the conditional %if statement by using the %by statement which increments the counter by 2 instead of the default 1 like this :

 

%macro m;
	%do i = 1 %to 3 %by 2;
		data dat&i;
			set sashelp.class;
		run;
	%end;
%mend m;

%m;
Super User
Posts: 9,662

Re: %GOTO vs CONTINUE in regular DO LOOP

Agree with @KurtBremser. Do not use GOTO statement either in Macro or in Data step ,which will mess your code up and make your code hard to read and tend to generate tons of errors. Any of C programmer knew that. Use DO WHILE() / DO UNTILE() instead .

Solution
‎02-28-2016 01:40 PM
Respected Advisor
Posts: 3,777

Re: %GOTO vs CONTINUE in regular DO LOOP

Yes your %GOTO does simulate the nonexistent %CONTINUE. 

 

GOTO is the most basic branching construct. CONTINUE and LEAVE statements are just GOTO in with implied labels.

 

Before there was DO in all its forms there was GOTO.  All DO forms can be coded with GOTO, at the heart of those structures the much maligned GOTO is toiling away unseen.

 

GOTO doesn't make programs bad, programmers make programs bad.

 

 

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 1456 views
  • 4 likes
  • 5 in conversation