Sample 43607: How to check for the existence of an environment variable
This macro uses the UNIX command ENV in a FILENAME PIPE along with various other SAS functions to retrieve a list of all environment variables. The list is checked against the environment variable passed to the macro. If the variable is found, the word yes is returned. If the variable is not found, the word no is returned.
This is for the UNIX operating system.
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.
/** This particular test checks to see if the environment variable logname **/
/** exist. Just replace logname in the macro call with the environment **/
/** variable you want to check and yes will be returned if it exist, else **/
/** no is returned. **/
%macro check(var);
%let filref=myenv;
%let dsid=%sysfunc(filename(filref,env,pipe,lrecl=32767));
%let fid=%sysfunc(fopen(&filref,s));
%do %while(%sysfunc(fread(&fid)) = 0);
%let rc=%qsysfunc(fget(&fid,c));
%let temp=%scan(%superq(c),1,=);
%if %upcase(&temp)=%upcase(&var) %then %do;
%let flag=1;
%goto exit;
%end;
%else %let flag=0;
%end;
%exit:
%let rc=%sysfunc(fclose(&fid));
%let rc=%sysfunc(filename(filref));
%if &flag=1 %then yes;
%else no;
%mend check;
%put Does env var exist: %check(logname);
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.
/** log results **/
Does env var exist: yes
This macro enables you to check for the existence of a system environment variable on UNIX.
Date Modified: | 2011-06-29 10:51:16 |
Date Created: | 2011-06-28 08:58:36 |
Operating System and Release Information
SAS System | Base SAS | HP-UX IPF | | |
IRIX | | |
HP-UX | | |
AIX | | |
64-bit Enabled Solaris | | |
ABI+ for Intel Architecture | | |
64-bit Enabled HP-UX | | |
64-bit Enabled AIX | | |
Linux | | |
Linux for x64 | | |
Linux on Itanium | | |
OpenVMS Alpha | | |
OpenVMS on HP Integrity | | |
Solaris | | |
Solaris for x64 | | |
Tru64 UNIX | | |