HomeContact
Check if a Specified Object Exists
July 30, 2021
1 min

Table Of Contents

01
Check if a SAS dataset exists or not.
02
Check for SAS Macro Variables

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.;
%else
%put Data set &name does not exist.;
%mend dsnexits;
%dsnexits(&dsname);```
### 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.
```sas
%let view=classs;
%macro viewexits(name);
%if %sysfunc(exist(&name, VIEW)) %then
%put &name exist.;
%else
%put &name does not exist.;
%mend viewexits;
%viewexits(&dsname);```
## 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.**
```sas
%macro VarExist(ds, var);
%if %sysfunc(exist(&ds, DATA)) %then %do;
%let dsid = %sysfunc(open(&ds));
%if %sysfunc(varnum(&dsid, &var)) > 0 %then
%put "NOTE": Var &var exists in `&ds;`
%else
%put "ERROR": Var &var does not exists in `&ds;`
%end;
%else
%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](https://www.9to5sas.com/sas-data-access-functions/) and complete the code of [Macro To Check If A Variable Exists In SAS Dataset.](https://www.9to5sas.com/variable-exists-sas/)
## Check if a file exists
[Fileexits](https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n06xm8hwk0t0axn10gj16lfiri43.htm) 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.
```sas
%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.;
%else
%put "ERROR": The external file &filepath does not exist.;
%mend fileexists;
%fileexists(&fpath);```
## Check if File References Are Valid.
The [FEXISTS](https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0gh473azqo3han1edlhbyt04gxa.htm) 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](https) 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](https) both exist.
```sas
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.;
%else
%put %sysfunc(sysmsg());
%mend filerefexists;
%filerefexists(&fref);```
## Check if Library references exist.
The [libref](https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0pe5ebdcgjnxkn1cgx90o5cla9a.htm) dataset function is used to locate an open [library](https://www.9to5sas.com/sas-libraries/) reference.** The LIBREF function returns 0 if the libref has been assigned or returns a nonzero value if the libref has not been set.**
```sas
%macro libexits(libref);
%if %sysfunc(libref(&libref)) %then
%put %sysfunc(sysmsg());
%else
%put "NOTE": &libref exists;
%mend libexits;
%libexits(sashelp);```
## 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.**
```sas
proc SQL; create table test as
select libname, fmtname, source, fmttype
from dictionary.formats where source='B' or source='C';
quit;```
![](../../assets/2021/07/img_6102368281b86.png)
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](https) article.
For more information on the user-defined format, see our guide on [Proc Format In SAS](https).
## Check if macro exists.
The [%SYSMACEXIST]("https"://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n0xwysoo8i2j3kn13ls4thkn3xbp.htm) function is used to determine if a compiled [macro](https://www.9to5sas.com/sas-macros/) exists in work.sasmacr.
```sas%put %nrstr(%sysmacexist(test))=%sysmacexist(test);
%macro test;
%put this is a macro;
%mend;
%put %nrstr(%sysmacexist(test))=%sysmacexist(test);
%test;
%put %nrstr(%sysmacexist(test))=%sysmacexist(test);```
**"Output":**
```sas%sysmacexist(test)=0
%sysmacexist(test)=1
this is a macro
%sysmacexist(test)=1

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;
%test;```
**"Output":**
```sas%symglobl(x)= 1
%symlocal(x)= 0
%symexist(x)= 1
%symexist(y)= 1
%symexist(z)= 0```
Download the codes from [here](https://github.com/subhroster/9to5sas_Codes/blob/38e340808c475369019521c1e45a14b0cfc907c8/SAS%20programs/object_Exists.sas).
**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](https://www.instagram.com/9to5sas/) and [Facebook](https://www.facebook.com/9to5sas/).
export const _frontmatter = {"title":"Check if a Specified Object Exists","slug":"object-exists","date":"2021-07-30T09:05:13","modified":"2022-03-16T10:11:48","excerpt":"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.","author":"Subhro","authorSlug":"subhroster","categories":["SAS PROGRAMS"],"tags":[],"wordpressId":10115,"wordpressLink":"https://www.9to5sas.com/object-exists/","featuredImage":10208,"type":"post"}

Share


© 2025 9to5sas
AboutContactPrivacyTerms