BookmarkSubscribeRSS Feed
Citrine10
Obsidian | Level 7

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.

10 REPLIES 10
Kurt_Bremser
Super User

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;
Citrine10
Obsidian | Level 7
thank you Kurt, but what do I do in the instance where there is no observations as there is no records as in that case there wouldnt be an obs column
ERROR: Variable obs is not on file WORK.Data
Citrine10
Obsidian | Level 7
proc print data=data(where=(obs > 0));
ERROR: Variable obs is not on file WORK.data

Citrine10
Obsidian | Level 7
I also tried the below and get an error:

%let noobs = 0; /* in case dataset does not exist */
proc sql noprint;
select nobs into :noobs from mydata
quit;
%if &noobs. > 0
%then %do;

FILENAME MailBox EMAIL
TO=(jackie@gmail.com)

FROM='henr@gmail.com'
type='text/html'
SUBJECT='Table';

ODS html body=Mailbox style = noline;
ods listing close;


proc print data=mydata
style (report) = {background = white
font_face = "Verdana" font_size = 7pt just=left bordercolor=grey rules=All frame=box}
style (column) = {background = white CELLHEIGHT = 2.5%
font_face = "Verdana" font_size = 7pt just=left }
style (header) = {foreground = cx5e2750 font_face="Verdana"
font_size = 8pt just=left
};
title1 "Table";
run;
ods html close;
%end;


ERROR: The following columns were not found in the contributing tables: nobs.
Kurt_Bremser
Super User

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.

yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



ghosh
Barite | Level 11
%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;
Sajid01
Meteorite | Level 14

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: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 1660 views
  • 7 likes
  • 5 in conversation