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 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
1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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

4 REPLIES 4
Kurt_Bremser
Super User

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.

ChrisBrooks
Ammonite | Level 13

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

Agree with @Kurt_Bremser. 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 .

data_null__
Jade | Level 19

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.

 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 6564 views
  • 6 likes
  • 5 in conversation