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