DATA Step, Macro, Functions and more

Do not export dataset with 0 obs?

Reply
Occasional Contributor
Posts: 19

Do not export dataset with 0 obs?

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

SAS Employee
Posts: 104

Do not export dataset with 0 obs?

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.

Super Contributor
Posts: 1,636

Re: Do not export dataset with 0 obs?

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

Super User
Posts: 9,677

Re: Do not export dataset with 0 obs?

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

Ask a Question
Discussion stats
  • 3 replies
  • 876 views
  • 0 likes
  • 4 in conversation