Sample 25075: A way to determine whether a macro variable exists
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.
/* Version SAS 9.1 and above */
/* Note: The & (ampersand) does not precede the macro variable name in */
/* the argument to the function. */
%let check=%symexist(x);
/* Prior to SAS 9.1 you can use the following macro to determine if a */
/* macro variable exists: */
%macro check(mvar);
%local i tmp;
%let dsid=%sysfunc(open(sashelp.vmacro));
%let num=%sysfunc(varnum(&dsid,name));
%do %until(&ob = -1);
%let i=%eval(&i+1);
%let ob=%sysfunc(fetchobs(&dsid,&i));
%let val=%sysfunc(getvarc(&dsid,&num));
%if &val = %upcase(&mvar) %then %do;
%let ob = -1;
%let tmp=yes;
%end;
%else %do;
%let tmp=no;
%end;
%if &ob=-1 %then %do;
&tmp
%end;
%end;
%let rc=%sysfunc(close(&dsid));
%mend check;
/* Check for the existence of the macro variable abc */
%put Does the macro variable exist: %check(abc);
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 log
Does the macro variable exist: no
Beginning with SAS 9.1, the %SYMEXIST function can be
used to determine if a macro variable exists. The function
returns a value of 1 if the macro variable exists and a
value of 0 if it does not.
| Type: | Sample |
| Topic: | SAS Reference ==> Macro
|
| Date Modified: | 2005-08-13 03:02:41 |
| Date Created: | 2005-01-26 16:17:17 |
Operating System and Release Information
| SAS System | Base SAS | All | 8 TS M0 | n/a |