To retrieve the name of the current program when you are running SAS® in batch mode, you can issue the following statement:
If you are running SAS interactively, the following code retrieves the path and the name of the current program:
/* NOTE: If more than one editor window is open, then the STOP statement will need
to be commented out from the macro. SASHELP.VEXTFL creates a unique
fileref for each editor window that is open. Removing the STOP allows
us to retrieve the LAST file opened. */
%macro pname;
%global pgmname;
%let pgmname=;
data _null_;
set sashelp.vextfl;
if (substr(fileref,1,3)='_LN' or substr
(fileref,1,3)='#LN' or substr(fileref,1,3)='SYS') and
index(upcase(xpath),'.SAS')>0 then do;
call symput("pgmname",trim(xpath));
stop;
end;
run;
%mend pname;
%pname;
%put pgmname=&pgmname;
Beginning with SAS®9, the above macro is not needed when using SAS in the Windows operating environment. There is a new environment variable for the Enhanced Editor named SAS_EXECFILENAME, and you can retrieve the current program by issuing this statement:
There is also an environment variable for the Enhanced Editor named SAS_EXECFILEPATH that contains the full path of the submitted program or catalog entry. The full path includes the folder and the filename.
The environment variables SAS_EXECFILENAME and SAS_EXECFILEPATH are available only in the Windows operating environment running within the Enhanced Editor.
If you are using SAS® Enterprise Guide® or SAS® Studio, the macro variable _SASPROGRAMFILE can be used. This macro variable returns the full path and filename of the SAS program that is currently being run. This macro variable is available only for SAS program files that are saved on the same server on which your SAS Studio code or SAS Enterprise Guide session is running. For example: