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
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.
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)
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)
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.
Thanks for your help Reeza
I look proc compare, it´s seems a better solution.
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.
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,
Done
Thanks to all for the help.
Regards.
Please consider marking the question as answered for future users.
Sorry
It´s already done.
Regards
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.