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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
pradeepalankar
Obsidian | Level 7

%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

5 REPLIES 5
pradeepalankar
Obsidian | Level 7

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

Astounding
PROC Star

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.

bill0101
Calcite | Level 5

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;


LinusH
Tourmaline | Level 20

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
Patrick
Opal | Level 21

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 23932 views
  • 6 likes
  • 6 in conversation