DSNEXST Statement: z/OS

Checks to see whether the specified physical file exists and is available.
Valid in: Anywhere
z/OS specifics: all


DSNEXST 'physical-filename ';

Required Argument

'physical-filename '
is the name of a physical file. Quotation marks around the name are optional. However, the data set name must always be fully qualified. In this case, physical-filename cannot specify a UNIX System Services file.


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;
%else %do;
   disp=(new,catlg) space=(cyl,(1,1)) blksize=6160
      dsorg=ps recfm=fb lrecl=80 unit=disk
%mend 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;
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;
  %else %do;
    %put &loc exists;

See Also

SAS Macro Language: Reference