Sample 26157: Print information to the Output window if data set is empty
This macro uses PROC PRINT to print the data set if it contains observations or DATA _NULL_ to write a message to the Output window if the data set has zero observations.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* Create a dummy data set containing an observation */
data one;
x=1;
run;
/* Create an empty data set */
data two;
stop;
run;
/* This macro prints the data set if it contains observations or prints */
/* a message if the data set has zero observations. */
options mprint;
%macro drive(dsn);
/* Open the data set to be read */
%let dsid=%sysfunc(open(&dsn));
/* Determine the number of observations in the dataset */
%let cnt=%sysfunc(attrn(&dsid,nobs));
/* Close the data set */
%let rc=%sysfunc(close(&dsid));
/* If the data set is not empty, issue a PROC PRINT */
%if &cnt ne 0 %then %do;
proc print data=&dsn;
title "This is data from data set &dsn";
run;
%end;
/* Otherwise, write to the output window */
%else %do;
data _null_;
title;
file print;
put _page_;
put "Data set &dsn is empty.";
run;
%end;
%mend;
/* Test the macro with the non-empty data set */
%drive(one)
/* Test the macro with the empty data set */
%drive(two)
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
PROC PRINT results from first macro invocation
Obs x
1 1
DATA _NULL_ results from second macro invocation
Data set two is empty.
Use macro logic to conditionally display a message in the Output window if a data set has zero observations.
Type: | Sample |
Topic: | SAS Reference ==> Macro
|
Date Modified: | 2007-04-28 03:02:52 |
Date Created: | 2007-04-28 03:02:52 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |