Good morning all,
I have a bunch of code which creates and exports datasets from a larger data collection system. Sometimes these datasets contain zero observations. At this point it is easiest to allow the whole program to run and create all of the datasets...
Is there a way to have proc export NOT export any datasets with zero obs?
Thanks,
GST
Probably a little export macro would be the best approach. Something like this perhaps:
%macro ExportMe(DSN,path=C:\MyFiles); %local Libname Memname nlobs; %let DSN=%QUPCASE(&DSN); %let Libname=%qscan(&DSN,1,.); %let Memname=%qscan(&DSN,2,.); proc sql noprint; select nlobs into :nlobs from dictionary.tables where libname="&libname" and memname="&memname" ; quit; %if &nlobs=0 %then %do; %PUT NOTE: &DSN had zero observations; %return; %end; PROC EXPORT DATA=&DSN OUTFILE= "&Path\&Memname..xls" DBMS=EXCEL REPLACE; SHEET="&Memname"; NEWFILE=YES; RUN; %mend;
You would then export your data sets using macro calls something like this:
%ExportMe(SASHELP.CLASS);
If there are zero observations, you just get a note in the log Otherwise the data set is exported to an excel file of the same name as the dataset.
Hi,
I created a macro for you.
/* create sample datasets, dsn4 with 0 observation */
data dsn1 dsn2 dsn3;
do a=1 to 3;
output;
end;
run;
proc sql;
create table dsn4 like dsn3;
quit;
%macro export(lib,path);
proc sql noprint;
select memname into: dnames separated by ' '
from dictionary.tables
where libname=%upcase("&lib");
quit;
%do i=1 %to %sysfunc(countw(&dnames));
%let dsn=%scan(&dnames,&i);
%put &dsn;
%let dsid = %sysfunc(open(&dsn));
%let origN = %sysfunc(attrn(&dsid, nobs));
%let rc = %sysfunc(close(&dsid));
%if &orign> 0 %then %do;
PROC EXPORT DATA=&lib..&DSN
OUTFILE= "&path\&dsn..xls"
DBMS=EXCEL REPLACE;
SHEET="&dsn";
NEWFILE=YES;
RUN;
%end; %end;
%mend;
%export(work,c:\temp)
Linlin
you can query dictionary table to find which table has 0 obs.
libname x v9 'c:\'; proc sql ; select memname from dictionary.tables where libname='X' and nobs ne 0; select memname into : a1 - : a&sqlobs. from dictionary.tables where libname='X' and nobs ne 0; quit; %put _user_; %macro csv; %do i=1 %to &sqlobs ; proc export data=x.&&a&i outfile="%sysfunc(pathname(x))&&a&i...csv " dbms=csv replace;run; %end; %mend csv; %csv
Ksharp
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.