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 ;
BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: 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 ;
BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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