Often Programmers are required to find if an object exists in SAS for validation or to execute certain codes dynamically. This article will help you to find if a specific object exists in SAS. The types of objects include datasets, external files, open libraries, file references, macros, macro variables, formats, informats, and specific variables in a dataset.

Check if a Specified Object Exists

Often, programmers need to determine whether or not an object exists in SAS or run specific SAS code dynamically.

This article will show you how to find out whether a specific SAS object exists. Examples of objects are datasets, external files, open libraries, file references, macros, macro variables, formats, and specific variables within a dataset.

Check if a SAS dataset exists or not.

You can use the Exists function to verify if a SAS dataset exists, and it will return 0 when the dataset is not found and 1 if the dataset does exist.

1. Verifying the Existence of a Data Set

The code below is used to check if the SAS dataset class exists in the SASHELP library.

%let dsname=sashelp.class;

%macro dsnexits(name);

%if %sysfunc(exist(&name, DATA)) %then
   %put Data set &name exist.;


   %put Data set &name does not exist.;

%mend dsnexits;


2. Verifying the Existence of a Data View 

Similar to checking the existence of a SAS dataset, you can also verify if a SAS View exists or not.

%let view=classs;

%macro viewexits(name);

%if %sysfunc(exist(&name, VIEW)) %then

  %put &name exist.;


  %put &name does not exist.;

%mend viewexits;


Check if a Variable exists in a SAS dataset.

The OPEN and VARNUM functions can be used to check if the variable exists in a SAS dataset.

The OPEN function opens a SAS data set, and the VARNUM function returns the variable’s position in the data set.

If the variable does not exist, then VARNUM returns to 0. If the result is greater than 0, the variable exists in the dataset.

%macro VarExist(ds, var);
%if %sysfunc(exist(&ds, DATA)) %then
  %let dsid = %sysfunc(open(&ds));

    %if %sysfunc(varnum(&dsid, &var)) > 0 %then
      %put NOTE: Var &var exists in &ds;
      %put ERROR: Var &var does not exists in &ds;

   %put ERROR: Data set &ds does not exist.;

%mend VarExist;

%VarExist(sashelp.classs, name);

For more information on SAS data access functions like open, Varnum and many more, see our guide on SAS Data Access Functions and complete the code of Macro To Check If A Variable Exists In SAS Dataset.

Check if a file exists

Fileexits function Verifies the existence of an external file by its physical name. FILEEXIST returns 1 if the external file exists and 0 if the external file does not exist.

%let fpath=/home/9to5sas/SAS Programs/Multiple_excel_files.sas;

%macro fileexists(filepath);
  %if %sysfunc(fileexist(&filepath)) %then
    %put NOTE: The external file &filepath exists.;
    %put ERROR: The external file &filepath does not exist.;

%mend fileexists;


Check if File References Are Valid.

The FEXISTS function Verifies the existence of an external file that is associated with a fileref. FEXIST returns 1 if the external file associated with fileref exists and 0 if the file does not exist.

You can assign filerefs using the FILENAME statement or the FILENAME external file access function.

The fileref function Verifies whether a fileref has been assigned for the current SAS session.

  • A negative return code indicates that the fileref exists, but the physical file associated with the fileref does not exist.
  • A positive value indicates that the fileref is not assigned.
  • A value of zero indicates that the fileref and external file both exist.
filename mytest '/home/9to5sas/SAS Programs/Multiple_excel_files.sas';

%let fref=mytest;

  %macro filerefexists(fref);
    %if %sysfunc(fexist(&fref)) %then
      %put NOTE: The external file &fref exists.;
      %put %sysfunc(sysmsg());

  %mend filerefexists;


Check if Library references exist.

The libref dataset function is used to locate an open library reference. The LIBREF function returns 0 if the libref has been assigned or returns a nonzero value if the libref has not been set.

%macro libexits(libref);

  %if %sysfunc(libref(&libref)) %then 

    %put %sysfunc(sysmsg());

    %put NOTE: &libref exists;

%mend libexits;


Check existence of SAS formats

You can find the SAS Formats in the dictionary. format table. Formats can be temporary and stored in the WORK directory or permanent and stored in libraries.

A format or informat supplied by SAS will have a SOURCE value of B, and a format or informat that is user-defined will have a SOURCE value of C.

proc SQL;
 create table test as
select libname, fmtname, source, fmttype
dictionary.formats where source='B' or source='C'; 

Check if a Specified Object Exists

You can use SASHELP.VFORMAT to see the SAS formats and SASHELP.VCFORMAT to view the user-defined formats.

For a macro to check if a format exists or not, refer to this article.

For more information on the user-defined format, see our guide on Proc Format In SAS.

Check if macro exists.

The %SYSMACEXIST function is used to determine if a compiled macro exists in work.sasmacr.

%put %nrstr(%sysmacexist(test))=%sysmacexist(test); 
%macro test; 
%put this is a macro; 
%put %nrstr(%sysmacexist(test))=%sysmacexist(test); 
%put %nrstr(%sysmacexist(test))=%sysmacexist(test);




this is a macro


The first %SYSMACEXIST returns 0 and indicates that the test macro does not exist.

The second invocation indicates that the macro exists because it has been compiled via the %MACRO statement.

The third invocation also indicates that the macro exists. It does not matter if the macro has not yet been executed for %SYSMACEXIST to determine its existence.

Note that %SYSMACEXIST only checks for the existence of macros in work.sasmacr. Therefore, those macro catalogues are not searched if you have a macro catalog associated with the SASMSTORE= option or reside in SASHELP. Also, autocall macros are not considered to exist until they are referenced.

Check for SAS Macro Variables

Macro variables can be located in the local and global symbol tables, accessible through the functions below.

  • %symglobal
  • %symlocal
  • %symexits

The %symglobl and %symlocal functions will return a one if the macro variable exists and a 0 if it does not exist.

The %symexists function returns the existence of a macro variable irrespective of global or local and returns one of the following values:

  • 1 if the macro variable is found
  • 0 if the macro variable is not found
%let x=gloablvar;

%macro test;

%let y=localvar;

%put %nrstr(%symexist(x))= %symexist(x);

%put %nrstr(%symexist(y))= %symexist(x);

%put %nrstr(%symexist(z))= %symexist(z);

%mend test;



%symglobl(x)= 1

%symlocal(x)= 0

%symexist(x)= 1

%symexist(y)= 1

%symexist(z)= 0

Download the codes from here.

The Takeaway:

So, this was our side of verifying if an object exists in SAS. We hope that you must have found it helpful.

Moreover, if you have any other suggestions or techniques to find a particular object that exists, suggest us below the comment section. We would take those lists in our other blog posts.

Thanks for reading!

Please subscribe to our mailing list for weekly updates. You can also find us on Instagram and Facebook.

Every week we'll send you SAS tips and in-depth tutorials



Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.