Skip to content
9to5sas
  • Index
  • Glossary
Facebook Twitter Instagram Email RSS

  • Start Here
  • Base SAS
  • Advanced SASExpand
    • SAS Macros
    • PROC SQL
  • SAS/STATSExpand
    • SAS Analytics
    • Statistics
  • SAS Programs
9to5sas

9to5sas » SAS PROGRAMS » How to Print Empty SAS dataset information to the output window?

How to Print Empty SAS dataset information to the output window?

BySubhro Posted onAugust 25, 2022August 25, 2022 Last Updated:August 25, 2022
0 Comments

If the file is empty, the PROC PRINT can read it, but nothing is printed, not even the headers of the report….

If the file is empty, the PROC PRINT can read it, but nothing is printed, not even the headers of the report. This article shows a workaround to Print Empty SAS dataset information to the output window.

Method 1: Print empty SAS dataset using the data access function.

To print empty dataset information to the output window, you can data access functions like ATTRN and NLOBS.

data one;
x=1;
run;
data two;
stop;
run;

%macro print(dsn);
%let dsid=%sysfunc(open(&dsn));
    
 %if &dsid ne 0 %then %do;
  %let cnt=%sysfunc(attrn(&dsid,nlobs));
  %let rc=%sysfunc(close(&dsid));
   %if &cnt ne 0 %then %do;
    proc print data=&dsn;
     title "This is data from data set &dsn"; 
    run;
   %end;
   %else %do;
    data _null_;
     title;
     file print;
     put _page_;
     put "Data set &dsn is empty.";
    run;
   %end;
 %end;
 %else %put &dsn cannot be open.;
   
%mend print;
%print(one)
%print(two)

Below is the result for %print(one).

How to Print Empty SAS dataset information to the output window?

Results for %print(two).

Print empty SAS datasets
Empty SAS datasets

The OPEN function opens the data set passed to the macro, and the %IF condition ensures the data set has been successfully opened.

The ATTRN function and the NLOBS argument determine the number of observations in the data set that was opened in the OPEN function, and the value is passed into a macro variable named &CNT.

The %IF statement checks &CNT and PROC PRINT is executed when &CNT is not 0.

The DATA step is executed if the %IF statement is false. Using the FILE PRINT statement, you can direct the output produced by the PUT statements to the same file.

An error message is written to the log if %ELSE cannot open the data set.

Method 2: Conditionally Print empty SAS datasets or Non-Empty datasets using Proc Print

If the dataset is empty, we want to print a message in the output window that there is no data.

In this example, there are two possible scenarios. 

The first case is when the table is not empty, and then the dataset will be printed.

The second scenario occurs when a dataset has no data, and a “no data” message must be displayed.

data no_data;
msg = 'No Data';
run;

/*Case 1 - Data set with records*/
data tbl_1;
x=1;
run;

/*Case 2 - Data set without records*/
data tbl_2;
If 0;
run;

data _null_;
   dsnid=open('tbl_2');
   if dsnid then nobs=attrn(open('tbl_2'), 'nobs' );
   if nobs=0 then call execute('proc print data=no_data; run;');
   else  call execute('proc print data=tbl_2; run;');
run;

Result for Case 1 – dataset tbl_1

How to Print Empty SAS dataset information to the output window?

Result for Case 2 – dataset tbl2

How to Print Empty SAS dataset information to the output window?

We use the OPEN function to open the data set and the %IF condition to make sure the data set opened successfully.

The number of observations in the data set that was opened with the OPEN function is determined by the ATTRN function and the NOBS argument. This number is then sent to a macro variable called &CNT.

The IF statement and Call Execute are used to execute the Proc Print code inside the call execute routine.

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Check your inbox or spam folder to confirm your subscription.

Subhro

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

Facebook Twitter Linkedin

Post navigation

Previous Previous
How to determine if the external file is empty in SAS?
NextContinue
How to Loop through Dates in SAS?

SAS Tips in your inbox

Subscribe to 9to5sas for timely SAS tips and news delivered each month.
Learn about the latest articles, and code samples to keep your SAS skills fresh!

Your subscription is successful!

Recent Posts

  • Concatenate strings in SAS: The CAT Functions Demystified
  • 5 Techniques for Quickly Removing Leading Zeros in SAS
  • Troubleshoot Your Proc SQL Code Like a Pro with These 7 Automatic Macro Variables
  • 7 PROC SQL Options You Should Use to Debug Queries
  • How To Use The SAS Proc SQL Order By Statement?
9to5sas
  • Privacy Policy
  • Disclaimer
  • About
  • Contact
Facebook Twitter Instagram RSS Email Pinterest

Copyright © 2023 9TO5SAS, All rights reserved.

Scroll to top
  • 9to5sas Blueprint
  • About
  • About
  • Acceptable use policy
  • calculator
  • Confirm email
  • Contact
  • Contact
  • Cookie Policy
  • DISCLAIMER
  • Getting Started with SAS
  • Glossary
  • Index
  • Post #13801
  • Privacy Policy
  • Privacy policy
  • SAS Programs
  • Styles
  • Subscription confirmed
  • Terms and conditions
  • Thank You