BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
alepage
Barite | Level 11

Hello,

 

I have seen a lot of message on how to check if a dataset is empty but many of the proposed codes generate errors.

 

Here's my little contribution to the community.

I have test this code and it seems to works properly.

 

Please note that my dataset named base is empty (no column, no observations) and &records gives zero.

You can replace the dataset by sashelp.class to test with a non zero dataset.  It works fine with both situation.

 

you can use the macro variable &records with a conditionnal statement to do what you want.

 

data _null_; /*data step to check for number of records in your dataset*/
%global records;
%let records=0;
set base end=last;
if last then call symput("Records",put(_n_,8.)); /*_n_ is observation number in sas dataset*/
run;
%put &records;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

if it is zero ,then table is empty .

 

%let dsid=%sysfunc(open(sashelp.class));
%let nobs=%sysfunc(attrn(&dsid,nlobs));
%let dsid=%sysfunc(close(&dsid));

%put nobs= &nobs ;

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Nice, but for most (except DB libnames) datasets the observation count is already present in sashelp.vtables (or the SQL dictionary equivalent).  So if you really need a macro variable (and for most operations you wouldn't, only maybe fr create an output report):

data _null_;
  set sashelp.vtable (where=(libname="<yourlib>" and memname="<yourds>"));
  call symput("records",put(nobs,best.));
run;

If I need a report though, simpler to do:

data _null_;
  set sashelp.vtable (where=(libname="<yourlib>" and memname="<yourds>"));
  if nobs > 0 then call execute('%Do_Report');
run;
Ksharp
Super User

if it is zero ,then table is empty .

 

%let dsid=%sysfunc(open(sashelp.class));
%let nobs=%sysfunc(attrn(&dsid,nlobs));
%let dsid=%sysfunc(close(&dsid));

%put nobs= &nobs ;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 2 replies
  • 54848 views
  • 10 likes
  • 3 in conversation