BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Citrine10
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

What kind of script?

 

DATA step?

Macro?

PROC SQL?
Something else?

 

Show us your code so far.

--
Paige Miller
Citrine10
Obsidian | Level 7
How do I do it with both a Datastep option and a proc sql option?
Kurt_Bremser
Super User

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.

yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Citrine10
Obsidian | Level 7
I tried your code and it works great but when putting it in my code shared below I get this error:
ERROR: Expected %DO not found.
ERROR: Skipping to next %END statement.

code:

%if &filecount. = 0 %then %goto section1;
%if &filecount. = 1 %then %goto section2;

%section1:
data _when_count_is_0;
x=17;
run;

%goto theend;

%theend:
%put *** END ***;
%mend;
Kurt_Bremser
Super User

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:

Bildschirmfoto 2020-04-07 um 08.32.59.jpg

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.

yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Citrine10
Obsidian | Level 7
%include "C:/list_files.sas";

%let path_in=C:/FileCheck;

/*List of files*/
%list_files("C:/Files",txt)

%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 %goto section3;
%if &filecount. = 1 %then %goto section2;

%section3:
data _when_count_is_0;
x=17;
run;

%goto theend;

%theend:
%put *** END ***;
%mend;
yabwon
Onyx | Level 15

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

 

 

 

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Citrine10
Obsidian | Level 7

I tried but getting this error:

Citrine10_0-1643353963090.png

 

yabwon
Onyx | Level 15

... 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;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 11 replies
  • 1106 views
  • 1 like
  • 4 in conversation