DATA Step, Macro, Functions and more

%GOTO vs CONTINUE in regular DO LOOP

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
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,799

Re: %GOTO vs CONTINUE in regular DO LOOP

Posted in reply to SAS_inquisitive

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: 7,854

Re: %GOTO vs CONTINUE in regular DO LOOP

Posted in reply to SAS_inquisitive

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
Super Contributor
Posts: 441

Re: %GOTO vs CONTINUE in regular DO LOOP

Posted in reply to SAS_inquisitive

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: 10,044

Re: %GOTO vs CONTINUE in regular DO LOOP

Posted in reply to SAS_inquisitive

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,799

Re: %GOTO vs CONTINUE in regular DO LOOP

Posted in reply to SAS_inquisitive

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
  • 2022 views
  • 4 likes
  • 5 in conversation