BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
zsmith115
Calcite | Level 5

Is there a way to jump out of a SAS dataset if a condition is met?  For example, if I want to include an IF statement at the top of my code to determine if a condition is met, I want to jump over all code to the end of the program so none of the remaining code is executed.  I've looked at the GOTO statement but the label where the code jumps to has to be within the same data statement which isn't what I need it to do.  Below is a simplified example of the code:

 

data one;

set two;

If &have_file = 'No' then GOTO endhere;

run;

 

*******;

additional code that I want to skip if &have_file = 'No';

******;

 

endhere:;

run;

 

Any help would be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

One way to go if you're using a recent version of SAS9

%if %upcase(&have_file) ne NO %then
  %do;

    data one;
      set two;
    run;

    /*******
    additional code that I want to skip if &have_file = 'No';
    ******/

  %end;

View solution in original post

8 REPLIES 8
Quentin
Super User

You definitely can't jump out of a DATA step to a location outside of the DATA step.

 

You might want to look into the macro language, which can be used for conditional execution of SAS code.

 

In a data step, you can conditionally end the data step early.  So you could set a macro variable SkipCode=1 and end the data step early.

 

Then in the macro language you could have your skippable code inside of a %if &skipcode ne 1 %then %do block.  Or the macro language has a %GOTO statement which is another option.

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
ballardw
Super User

Is the value of &have_file the name of a variable in the data set? If not then the IF is not useful and probably the wrong If. If you are setting the macro variable to avoid executing the data set because of an external to the data set condition then the structure would be MACRO code

 

%if &have_file=NO %then %goto Skipit;
   data one;
       set two;
   end;

%Skipit:

HOWEVER the %goto is only allowed in Macro definition code. Which means defining some of the code involved in a %macro / %mend block, likely with parameters to pass into the macro such as the value of your &have_file macro variable. The calling the compiled macro after the definition. Which depending on everything you are may be doing could be a bit of work.

 

 

Tom
Super User Tom
Super User

RETURN will end the current iteration of the data step and start the next iteration.  If you don't want the implied OUTPUT at the end of the data step to run then use the DELETE statement instead.

 

STOP will end the data step immediately instead of waiting for the normal data step end. Note that most data steps actually end when they read past the end of the inputs in either the SET/MERGE/UPDATE statement or the INPUT statement.

Tom
Super User Tom
Super User

If you want to conditionally execute code you need to use MACRO statements.

You could use the first data step to set a macro variable than you can then use in the macro code.

For example if the trigger is that there are any observations in TWO where the value of X is larger than 5 you might do something like this:

%let any_found=0;
data _null_;
  set two;
  where x > 5 ;
  call symputx('any_found','1');
  stop;
run;

%if (&any_found) %then %do;
* other data steps and proc steps;
%end;

* end of program;
mbuchecker
Quartz | Level 8
If you there is absolutely no other code after your "endhere", in other words you simply want the whole program to stop, code: if &have_file='No' then abort return;

otherwise use the macro %IF suggestion that others wrote.
Michelle
SASKiwi
PROC Star

What is the real condition you are testing for? If it whether a certain data file exists or not, pretty much any computer task scheduler can do that within the tool so you don't need to code for it in SAS at all. It does mean that you will have to run your SAS job in batch mode though.

Patrick
Opal | Level 21

One way to go if you're using a recent version of SAS9

%if %upcase(&have_file) ne NO %then
  %do;

    data one;
      set two;
    run;

    /*******
    additional code that I want to skip if &have_file = 'No';
    ******/

  %end;
zsmith115
Calcite | Level 5

Thanks to everyone that replied with suggestions.  I was able to get my code to work using the %if %then %do logic.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 8 replies
  • 911 views
  • 1 like
  • 7 in conversation