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

Hi,

 

I've got a macro set up but I want SAS to only run it when the number of observations in the input dataset >0

 

So for example, for the code below, I only want it to run for datasets that aren't empty. Assume that dataset source2 is empty in this instance.

 

%macro abc(input);

data want;
set &input.;
run;

%mend abc;

%abc(source1);
%abc(source2);
%abc(source3);
1 ACCEPTED SOLUTION

Accepted Solutions
SuzanneDorinski
Lapis Lazuli | Level 10
options mprint;

data source1;
  set sashelp.class;
run;

data source2;
  input name;
  datalines;
   ;
run;

data source3;
  set sashelp.cars;
run;


%macro abc(input);

    proc sql noprint;
      select count(*) 
        into :obs_count
          from &input;
    quit;
    
    %if &obs_count gt 0 %then
      %do;
      
	    data want;
		  set &input.;
	    run;
	 %end;

%mend abc;

%abc(source1);
%abc(source2);
%abc(source3);

View solution in original post

3 REPLIES 3
SuzanneDorinski
Lapis Lazuli | Level 10
options mprint;

data source1;
  set sashelp.class;
run;

data source2;
  input name;
  datalines;
   ;
run;

data source3;
  set sashelp.cars;
run;


%macro abc(input);

    proc sql noprint;
      select count(*) 
        into :obs_count
          from &input;
    quit;
    
    %if &obs_count gt 0 %then
      %do;
      
	    data want;
		  set &input.;
	    run;
	 %end;

%mend abc;

%abc(source1);
%abc(source2);
%abc(source3);
Ltwo
Fluorite | Level 6

Thanks heaps

RW9
Diamond | Level 26 RW9
Diamond | Level 26
data _null_;
  set sashelp.vtable (where=(libname="WORK" and substr(memname,1,6)="SOURCE" and nobs > 0));
  call execute(cats('%abc (',catx('.',libname,memname),');'));
run;

Simplest form, you can change WORK (must be upcase) to yout lib and use the where to restrict tables/libraries - for instance if you dropped the lib, it would check any dataset source: from any lib.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 3072 views
  • 3 likes
  • 3 in conversation