Sample 24670: Determine if a data set exists and conditionally execute additional steps
This sample uses the EXIST function to determine if a SAS® data set exists. If the data set exists, execute a PROC PRINT step. If it does not exist, execute a DATA _NULL_ to write a message to the Listing destination stating that the data set does not exist.
The EXIST function returns a 1 if the data set exists, and a zero if the data set does not exist.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* If you attempt to print or manipulate the non-existent data set, */
/* an error is generated. To prevent the error, check to see if */
/* the data set exists, then conditionally execute the step(s). */
options mprint mlogic symbolgen;
%macro checkds(dsn);
%if %sysfunc(exist(&dsn)) %then %do;
proc print data = &dsn;
run;
%end;
%else %do;
data _null_;
file print;
put #3 @10 "Data set &dsn. does not exist";
run;
%end;
%mend checkds;
/* Invoke the macro, pass a non-existent data set name to test */
%checkds(sasuser.not_there)
/* Create a test data set and invoke the macro again, */
/* passing the data set name that does exist */
data a;
a=1;
run;
%checkds(work.a)
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
OUTPUT from first invocation of macro
Data set sasuser.not_there does not exist
RESULTS from second invocation of macro
Obs a
1 1
Dynamically determine if a data set exists.
Type: | Sample |
Topic: | Common Programming Tasks ==> Utilities SAS Reference ==> DATA Step SAS Reference ==> Macro
|
Date Modified: | 2007-04-17 03:02:55 |
Date Created: | 2004-09-30 14:09:02 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |