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

Hello

I would like to create dynamic sas program that SET multiple data sets (from fixed list of data sets).

The requirement is that If data set has zero rows then this data set will not be in SET statement.

What is the way to do it please?

In this example data set "wanted"  will be result of setting data sets:tbl2 and tbl3

Please notr that the reason that I dont want to include data set with zero rows in the set statement is that I dont want  to get variables from empty data set in the wanted data set. 

data tbl1;
set sashelp.iris;
stop;
z=PetalLength-10;
run;

data tbl2;
set sashelp.iris;
X=SepalWidth*100;
W=PetalLength*10;
run;

data tbl3;
set sashelp.iris(obs=10);
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

Usually the best approach is to know which variables you want to keep, and list them on a KEEP statement or option.

 

That said, if you have a function-style macro like %AnyObs (see https://support.sas.com/resources/papers/proceedings/proceedings/sugi26/p095-26.pdf ), then you can use that in an %IF statement to decide whether or not to list a dataset on the SET statement, e.g.

 

data want ;
  set _null_  /*protect against reading _last_ if none of below datasets have records*/
    %if %anyobs(tbl1) %then %do ; tbl1 %end ;
    %if %anyobs(tbl2) %then %do ; tbl2 %end ;
    %if %anyobs(tbl3) %then %do ; tbl3 %end ;
  ;
run ;
The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Since datasets with zero observations will not contribute any observations to the result anyway, this is not necessary. In fact, I find it even better to have the zero observations documented in the log, than not seeing a particular dataset there.

Ronein
Meteorite | Level 14

Will data sets with zero observations contribute to columns list?

I dont want to have columns that appear in data set with zero obs and dont appear in other data sets

A_Kh
Lapis Lazuli | Level 10

Let's say you have all these datasets in WORK library. We can use sashelp.vtable to subset datasets based on nobs. The idea is importing all datasets name with obs >0 into a macro variable for further use. 

Eg: 

data _null_;
	set sashelp.vtable end=eof;
	where libname eq 'WORK' and nobs>0;
	length datasets $ 200;
	retain datasets;
	datasets= catx(" ", datasets, memname); 
	if eof then do; 
		call symputx('list', datasets);
	end;
run; 
%put &=list;

 tables.PNGCapture.PNG

data want;
	set &list;
run; 
Quentin
Super User

Usually the best approach is to know which variables you want to keep, and list them on a KEEP statement or option.

 

That said, if you have a function-style macro like %AnyObs (see https://support.sas.com/resources/papers/proceedings/proceedings/sugi26/p095-26.pdf ), then you can use that in an %IF statement to decide whether or not to list a dataset on the SET statement, e.g.

 

data want ;
  set _null_  /*protect against reading _last_ if none of below datasets have records*/
    %if %anyobs(tbl1) %then %do ; tbl1 %end ;
    %if %anyobs(tbl2) %then %do ; tbl2 %end ;
    %if %anyobs(tbl3) %then %do ; tbl3 %end ;
  ;
run ;
The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.

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!

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