hi I would like for my dataset to not be printed to that it doesnt send an email when there are no records. The below is not working though:
proc print data=mydata; (where noobs > 0); run;
ERROR 180-322: Statement is not valid or it is used out of proper order.
You can either have a WHERE= dataset option:
proc print data=mydata (where=(noobs > 0));
run;
or a WHERE statement:
proc print data=mydata;
where noobs > 0;
run;
but if you want to only run the PROC PRINT when your dataset has observations, you must do this:
%let noobs = 0; /* in case dataset does not exist */
proc sql noprint;
select nobs int :noobs from dictionary.tables
where libname = "WORK" and memname = "MYDATA";
quit;
%if &noobs. > 0
%then %do;
proc print data=mydata;
run;
%end;
Please post your complete log of trying the code I gave you.
You cannot use a variable in the WHERE that does not exist in the dataset.
Please explain in clear terms what you want to do.
You do not use my code. My code reads the metadata of the MYDATA table from DICTIONARY.TABLES. Please study my code in detail, and, as already asked, please explain in detail what you want to do.
An alternative to dictionary tables:
%macro printMe(DS);
data _null_;
if nobs then call execute (" proc print data=&DS.; run; ");
stop;
set &DS. nobs=nobs;
run;
%mend printMe;
%printMe(sashelp.class);
data test;
x = 42;
stop;
run;
%printMe(test);
All the best
Bart
%macro prnt;
%if %sysfunc(exist(work.mydata)) %then %do;
proc print data = work.mydata;
run;
%end;
%else %put dataset does not exist;
%mend prnt;
%prnt;
There are many approaches to solve your issue. I have used the following and it should serve your purpose i.e., print the file if there are observations in the dataset and not when empty.
I have created an empty dataset called class from sashelp.class and used it to illustrate my code.
/*Creating an empty dataset*/
proc sql;
create table class like sashelp.class;
quit;
%macro print_if_not_empty(dataset_name);
/* finding the number of observations.*/
data _null_;
set &dataset_name. nobs=obs;
call symputx('nobs',nods);
run;
%if %sysfunc(symexist(nobs)) %then %do;
proc print data=&dataset_name.;
run;
%end;
%else %do;
%put ****The dataset &dataset_name. is empty *****;
%end;
%mend;
/*Call the macro */
%print_if_not_empty(class)
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 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.
Ready to level-up your skills? Choose your own adventure.