Hi there
How to I instruct my script to jump to a certain point in the script when a certain condition is met?
e.g.
case when count is 0 then goto section1
case when count is 1 then goto section4
etc.
You could try %goto macro statement:
%macro myCode(count);
%put *** START ***;
%if &count. = 0 %then %goto section1;
%if &count. = 1 %then %goto section4;
%section1:
data _when_count_is_0;
x=17;
run;
%goto theend;
%section4:
data _when_count_is_1;
x=42;
run;
%goto theend;
%theend:
%put *** END ***;
%mend;
%myCode(0)
%myCode(1)
Bart
What kind of script?
DATA step?
Macro?
PROC SQL?
Something else?
Show us your code so far.
SQL is the Structured Query Language and does not have the concept of jumping around in the code.
As already asked, please supply examples for code where you think you need the GO TO.
I can tell you from my experience that I have coded more than 2 decades in SAS (quite successfully, I might say) without ever needing a GO TO.
You could try %goto macro statement:
%macro myCode(count);
%put *** START ***;
%if &count. = 0 %then %goto section1;
%if &count. = 1 %then %goto section4;
%section1:
data _when_count_is_0;
x=17;
run;
%goto theend;
%section4:
data _when_count_is_1;
x=42;
run;
%goto theend;
%theend:
%put *** END ***;
%mend;
%myCode(0)
%myCode(1)
Bart
Please show the whole macro code, from %MACRO to %MEND, and also include the macro call that results in the problem.
Use the "little running man" button right next to the one indicated to post SAS code:
The subwindow will keep all the code formatting with a fixed-space font, and it will provide coloring similar to the Enhanced Editor that SAS on Windows and the Enterprise Guide use.
From the code you shared I can't see what's the issue. Please do as @Kurt_Bremser wrote and share more of your code.
Bart
1) You did very basic error, your code doesn't have the macro head:
%macr yourMacroName(parameters...);
2) with such a definition you need to use a macro call after defining it:
%yourMacroName(parameters...)
3) With your example it look like you don't need the %GOTO "cannon", the regular conditional %IF will do the job.
Here is the code modified, with changes pointed by "arrows"
%include "C:/list_files.sas";
%let path_in=C:/FileCheck;
/*List of files*/
%list_files("C:/Files",txt)
%macro test(); /* <- macro head */
%let filecount = 0;
%if %sysfunc(exist(tables))
%then %do;
/*Count number of files*/
proc sql;
create table filecount as
select count(name) into: filecount
from tables;
quit;
%end;
%if &filecount. = 0 %then /* <- replace GOTO with IF condition */
%do;
data _when_count_is_0;
x=17;
run;
%end;
%mend;
%test() /* <- call the macro */
Bart
I tried but getting this error:
... where you get this... show us a meaningful part of the log...
Bart
P.S. from what I know about such situations you may have somewhere in your code a letter/symbol standing just after a quote symbol, like:
put "some text"a_letter;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.