Help using Base SAS procedures

checking for the existence of a dataset

Reply
N/A
Posts: 0

checking for the existence of a dataset

Hi,

I want to check if a dataset exists before I run a datastep that will try to use that dataset. If the dataset does not exist then, I don't want to run my data step as it will cause errors and therefore I may not be able to run my code in an automated environment.

Can anyone help?
Super Contributor
Posts: 359

Re: checking for the existence of a dataset

Posted in reply to deleted_user
Try the EXIST() function.
Or look into the dictionary tables for SQL or the sashelp.vxxxx views..
Super Contributor
Posts: 474

Re: checking for the existence of a dataset

EXIST function will test the existence of the dataset, but you will need some macro coding to conditionally bypass the execution of the datastep.

Cheers from Portugal

Daniel Santos @ www.cgd.pt
Super Contributor
Super Contributor
Posts: 3,174

Re: checking for the existence of a dataset

Posted in reply to DanielSantos
Not necessarily, a conditional use of %INCLUDE with a temp file (or CALL EXECUTE) also works, without needing the SAS macro language.

Scott Barry
SBBWorks, Inc.
Super Contributor
Posts: 474

Re: checking for the existence of a dataset

Posted in reply to deleted_user
Oh yes, call execute will do the trick (and a lot of other tricks actually, a powerful statement indeed) , but I prefer to avoid it, has the coding gets "messier" and more difficult to maintain.

I understand the use of %INCLUDE, but I'm curious on how would you conditionally execute it without the use of macro coding.

The only way that I think of, would be to always produce a temp file (and include it), with one of the two possible codes, being one empty (if the dataset does not exist) and the other with the desired code to manipulate the existing dataset.

Again, I find the latest a little bit more complex than the macro coding, but then again, all valid solutions.

Cheers from Portugal

Daniel Santos @ www.cgd.pt
Super Contributor
Super Contributor
Posts: 3,174

Re: checking for the existence of a dataset

Posted in reply to DanielSantos
The temp file approach is one I use frequently - always execute %INCLUDE ; but the contents of the temp file is conditional based on prior SAS code.

And, also consider that when not using macro code, the SAS line numbers are assigned for reference.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: checking for the existence of a dataset

Thanks guys,

I used:

%macro check;

%if %sysfunc(exist(dataset))=0 %then %do;

>> blah blah <<

%end;
%mend;

%check;
Super Contributor
Super Contributor
Posts: 3,174

Re: checking for the existence of a dataset

Posted in reply to deleted_user
Straight from the SAS-hosted documentation - link below:

SAS Language Reference: Dictionary, EXIST Function
http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000210903.htm

Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 7 replies
  • 354 views
  • 0 likes
  • 4 in conversation