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;
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 ;
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.
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
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;
data want;
set &list;
run;
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 ;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.