BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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?
7 REPLIES 7
Flip
Fluorite | Level 6
Try the EXIST() function.
Or look into the dictionary tables for SQL or the sashelp.vxxxx views..
DanielSantos
Barite | Level 11
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
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
DanielSantos
Barite | Level 11
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
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
deleted_user
Not applicable
Thanks guys,

I used:

%macro check;

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

>> blah blah <<

%end;
%mend;

%check;
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 3433 views
  • 0 likes
  • 4 in conversation