BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi,

I'm looking for a simple solution to check is table empty or not.

Thanks
8 REPLIES 8
deleted_user
Not applicable
I have a solution! I think this is the best one 🙂

data _null_;
call symput('obscnt',0);
set work.out;
call symput('obscnt',_n_);
stop;
run;

%If &obscnt=0 %then %do;
%put 'There are no records in a dataset.';
%end;
1162
Calcite | Level 5
Here's an alternate solution:

proc sql;
select "# of Obs in WORK.OUT: ", nobs
from dictionary.tables
where libname='WORK'
and memname = 'OUT';
quit;
deleted_user
Not applicable
That's cool, but I don't like to be depended of this "dictionary"... Ofcourse, it's up to you... 🙂
advoss
Quartz | Level 8
Why don't you like to depend upon the "dictionary" tables? Unless you are using where clause in a proc or data step, the information provided by the dictionary tables is as good as SAS is.
deleted_user
Not applicable
Some people seem to have the impression that querying the Dictionary views is a slow process. In all cases I have seen, this is a result of poorly constructed SQL code and a SAS session that includes foreign (Sql server, Oracle, DB2 etc) libraries.

A well constructed SQL query such as that shown should have no performance issues.
LawrenceHW
Quartz | Level 8
If people don't like querying dictionary tables (and I don't understand why not) you can always use the following code:

%LET dsid=%SYSFUNC(OPEN(work.dataset));
%LET nobs=%SYSFUNC(ATTRN(&dsid.,NOBS));
%LET rc=%SYSFUNC(CLOSE(&dsid.));

The number of observations is then held in a macro variable [NOBS].

%IF &Nobs. EQ 0 %THEN %DO; .... %END; etc. etc. Message was edited by: LawrenceHW
Cynthia_sas
SAS Super FREQ
This is an elegant solution and one that I use a lot.

Just one warning that any %IF logic cannot be in open code -- but must be in a SAS Macro program.

cynthia
Akhil_Vijayan
Fluorite | Level 6
Am try outed this but it shows as "There are no records in a dataset" also when the data set contain observation

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!

Health and Life Sciences Learning

 

Need courses to help you with SAS Life Sciences Analytics Framework, SAS Health Cohort Builder, or other topics? Check out the Health and Life Sciences learning path for all of the offerings.

LEARN MORE

Discussion stats
  • 8 replies
  • 34139 views
  • 3 likes
  • 6 in conversation