DSNEXST is a global
statement. The first time the statement is issued, it performs one
of the following actions:
-
creates the macro variable &SYSDEXST
and assigns a value of 1 to it if the data set exists and is available
for allocation
-
creates the macro variable &SYSDEXST
and assigns a value of 0 to it if the data set does not exist.
Note: The DSNEXST statement causes
SAS to perform a
z/OS data set dynamic allocation. If the specified
data set is on a removable volume, such as a tape, then it will be
mounted. Data sets that have been migrated by HSM (the
z/OS Hierarchical
Storage Manager) will be recalled. To avoid these problems, use the
DSNCATLGD function.
The following example
allocates a data set differently depending on whether the data set
already exists or not.
%macro mydsn;
dsnexst 'my.data.set';
filename myds 'my.data.set'
%if &sysdexst %then %do;
disp=old;
%end;
%else %do;
disp=(new,catlg) space=(cyl,(1,1)) blksize=6160
dsorg=ps recfm=fb lrecl=80 unit=disk
volser='MYVOL';
%end;
%mend mydsn;
%mydsn
The next example shows
how you can submit some SAS statements if a data set already exists
and bypass them if it does not.
%macro copylib;
dsnexst 'my.data.library';
%if &sysdexst %then %do;
libname mylib 'my.data.library' disp=shr;
proc copy in=mylib out=work;
run;
%end;
%mend;
%copylib
In situations where
there could be more than one user of the data set, the following example
shows how you can use the &SYS99ERR automatic macro variable to
distinguish between “data set does not exist” and “data
set exists but is not available.”
%macro dsexist(loc);
dsnexst &loc;
%if &sysdexst=0 and &sys99err=1708
%then %do;
%put &loc does not exist;
%end;
%else %do;
%put &loc exists;
%end;
%mend;
%dsexist(my.data.set)