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 is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

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 is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 962 views
  • 3 likes
  • 4 in conversation