BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SergioSanchez
Calcite | Level 5

Hi all

I have the following code:

data a;

set b;

if variable x ne variable y;

run;

proc export data = a dbms= excel

outfile = path to safe file;

sheet = "test";

run;

Is there any option to run the proc export only if the number of observations is greater than 0?

This code is repeated comparing two variables each time and most of the time there are no observarions in the output.

thanks to all

Regards

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Not sure why you need all that macro code and file open close etc.  If you want to do a whole library of datasets where observations is greater than 0 then do:

data _null_; 

  set sashelp.vtable (where=(libname="your libname here" and nobs=0) keep=memname);

  call execute('proc export data=your_libname.'||strip(memname)||' dbms= excel

                                   outfile="s:\temp\rob\export_file_'||strip(memname)||'.xls;

                  sheet = "test";

                run;');

run;

This will select any dataset with > 0 observations in the libname and create export statements for each one.

View solution in original post

9 REPLIES 9
Reeza
Super User

You could wrap it in a macro. If you're repeating this multiple times, it probably should be in a macro anyways.

data class;

set sashelp.class;

if age in (13, 14) then height=age;

run;

%macro export_conditionally(var1, var2);

data a;

set class;

if &var1 eq &var2;

run;

proc sql noprint;

select count(*)  into :count from a;

quit;

%if &count ne 0 %then %do;

    proc export data=a dbms=xls

    outfile="C:\_localdata\temp.xls";

    sheet="test";

    run;

%end;

%mend;

%export_conditionally(weight, height)

%export_conditionally(age, height)

stat_sas
Ammonite | Level 13

Hi,

This may be helpful in your desired solution.

Thanks,

Naeem

%macro export (dsname);

%let dsid=%sysfunc(open(&dsname));

%let nobs=%sysfunc(attrn(&dsid,nobs));

%let rc=%sysfunc(close(&dsid));

title "Num Obs: &nobs";

%if &nobs>0 %then %do;

data a;

set &dsname;

if x ne y;

run;

proc export data = a dbms= excel

outfile = 'path to safe file';

sheet = "test";

run;

%end;

%mend export;

%export(b)

Reeza
Super User

The open/close is probably a better way to get the count.

However, if you're comparing variables and looking for identical values you may want to look into proc compare.

SergioSanchez
Calcite | Level 5

Thanks for your help Reeza

I look proc compare, it´s seems a better solution.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Not sure why you need all that macro code and file open close etc.  If you want to do a whole library of datasets where observations is greater than 0 then do:

data _null_; 

  set sashelp.vtable (where=(libname="your libname here" and nobs=0) keep=memname);

  call execute('proc export data=your_libname.'||strip(memname)||' dbms= excel

                                   outfile="s:\temp\rob\export_file_'||strip(memname)||'.xls;

                  sheet = "test";

                run;');

run;

This will select any dataset with > 0 observations in the libname and create export statements for each one.

SergioSanchez
Calcite | Level 5

Thanks EW9.

it´s seems is an easy way to achieve the goal.

Edit;

Sorry for the incovenience but  I try to run the following code:

data _null_; 
set sashelp.vtable (where=(libname="difiere" and nobs=0) keep=memname);
call execute('proc export data=difiere.'||strip(memname)||' dbms= excel
              outfile="c:\dirtest\export_file_'||strip(memname)||'.xls;
              sheet = "test";
              run;');

run;

and I revieve this error in the log:

ERROR: Variable libname is not on file SASHELP.VTABLE.

I don´t understand anything, I can open the vtable dataset and see the variable.

Why do I get this message?

Thanks,

SergioSanchez
Calcite | Level 5

Done

Thanks to all for the help.

Regards.

Reeza
Super User

Please consider marking the question as answered for future users.

SergioSanchez
Calcite | Level 5

Sorry

It´s already done.

Regards


hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 9 replies
  • 7443 views
  • 11 likes
  • 4 in conversation