Stop proceeding the rest of the program if certain condition is met.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Stop proceeding the rest of the program if certain condition is met.

Data Code1;

Set Test1;

Run;

Data Code2;

Set Test2;

Run;

Data Code3;

Set Test3;

Run;

I want the rest of the program to stop running if dataset Code1 is empty. Any help with this?


Accepted Solutions
Solution
‎01-27-2014 04:39 AM
Frequent Contributor
Posts: 106

Re: Stop proceeding the rest of the program if certain condition is met.

Posted in reply to sasthebest

%macro execute;

Data Code1;

Set test1;

Run;

proc sql;

select count(*) into :Count from code1;

quit;

%if &count>0 %then %do;

%put execute further;

Data Code2;

Set Test2;

Run;

Data Code3;

Set Test3;

Run;

%end;

%else %put exit;

%mend;

%execute;

View solution in original post


All Replies
Solution
‎01-27-2014 04:39 AM
Frequent Contributor
Posts: 106

Re: Stop proceeding the rest of the program if certain condition is met.

Posted in reply to sasthebest

%macro execute;

Data Code1;

Set test1;

Run;

proc sql;

select count(*) into :Count from code1;

quit;

%if &count>0 %then %do;

%put execute further;

Data Code2;

Set Test2;

Run;

Data Code3;

Set Test3;

Run;

%end;

%else %put exit;

%mend;

%execute;

Super User
Posts: 5,496

Re: Stop proceeding the rest of the program if certain condition is met.

Posted in reply to sasthebest

I would add one line, and change one line:

data Code1;

   if _n_=1 and done then call execute('endsas;');

   set Test1 end=done;

run;

The rest of the program doesn't have to change.

Note that this will close out any interactive use of SAS, such as an Enterprise Guide session.  So it is most appropriate for batch jobs.

Occasional Contributor
Posts: 17

Re: Stop proceeding the rest of the program if certain condition is met.

Posted in reply to sasthebest

Hello,

1.The exist() data step function judges the existence of a dataset without unnecessarily accessing data (especially when the dataset is huge).

2.To enable data step functions or routines in a macro, simply enclose it with %sysfunc().

3. If you need to store the existence state, use %global statement to declare a global variable and use %let to assign value.

The above concepts are demonstrated in the following test code. You can change it for your use. Hope this helps

Bill

/* data A1 does not exist while A2 exists */

data A2;

     input X @@;

     datalines;

1 2

;

run;

/* conditional execution */

%macro Conditional();

     /* get existence directly */

     %if %sysfunc(exist(A1)) %then %do;

           %put execute code here when A1 exists;

     %end;

     %else %do;

           %put execute code here when A1 does not exist;

     %end;

     /* store existence into a macro variable for further use */

     %global exist2;

     %let exist2= %sysfunc(exist(A2)); /* enclose to-be-resolved functions with %sysfunc() */

     %if &exist2 %then %do;

           %put execute code here when A2 exists;

     %end;

     %else %do;

           %put execute code herewhen A2 does not exist;

     %end;

%mend;

/* execute */

%Conditional();

/* existence of A2 is stored */

%put exist2=&exist2;


Super User
Posts: 5,424

Re: Stop proceeding the rest of the program if certain condition is met.

I like the use of data step functions, since they are as efficient as it can be. And wrapped into a macro, it can be as flexible that you want.

But if "is empty" means exists with no rows, perhaps you need to use the ATTRN() function instead.

Data never sleeps
Respected Advisor
Posts: 4,173

Re: Stop proceeding the rest of the program if certain condition is met.

Posted in reply to sasthebest

I would first develop the code and then wrap a macro with a %goto statement around it. Something like below:

%macro wrapper;

   <sas code>

   %if <condition> %goto exit;

   <more SAS code>

%exit:
%mend wrapper;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 6248 views
  • 4 likes
  • 6 in conversation