O'Reilly logo

SAS 9.4 Macro Language, 5th Edition by

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

If the data set could not be opened, %ELSE is executed, which uses %PUT to write a
note to the log.
%else %put &dsn cannot be open.;
Use the CLOSE function to close the SAS data set.
%let rc=%sysfunc(close(&dsid));
End the macro.
%mend lst;
Invoke the macro using the name of the SAS data set.
%lst(one)
Use the %PUT statement to write the contents of &X to the log.
%put macro variable x = &x;
Log
1 %let cnt=%sysfunc(attrn(&dsid,nvars));
2
3 /* Create a macro variable that contains all dataset variables */
4 %do i = 1 %to &cnt;
5 %let x=&x %sysfunc(varname(&dsid,&i));
6 %end;
7
8 /* Close the data set */
9 %let rc=%sysfunc(close(&dsid));
10
11 %mend lst;
12
13 /* Pass in the name of the data set */
14 %lst(one)
15
16 %put macro variable x = &x;
macro variable x = x y
Example 6: How to Retrieve the Program Name
That Is Currently Running in Batch Mode or
Interactively
Details
If you are running SAS interactively, the macro code in the Program section retrieves the
path and the name of the current program. If more than one editor window is open, then
the STOP statement needs to be commented out of the macro. SASHELP.VEXTFL
creates a unique fileref for each editor window that is open. Removing the STOP enables
you to retrieve the LAST file opened.
To retrieve the name of the current program when you are running SAS in batch mode,
you issue the following statement:
%put The current program is %sysfunc(getoption(sysin));
456 Appendix 5 SAS Macro Examples
The macro in the Program section below is not needed when using SAS in the Windows
operating environment. There is a new environment variable for the Enhanced Editor
named SAS_EXECFILENAME. You can retrieve the current program by issuing this
statement:
%put %sysget(SAS_EXECFILENAME);
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.
Program
%macro pname;
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 symputx('pgmname',xpath,'g');
stop;
end;
run;
%mend pname;
%pname
%put pgmname=&pgmname;
Program Description
Begin the macro definition.
%macro pname;
Begin a DATA step and reference the path information for external files (filerefs) by
using the SET statement and accessing the SASHELP.VEXTFL view.
data _null_;
set sashelp.vextfl;
Conditionally check each fileref to see whether the first three characters are _LN or #LN
or SYS and that the XPATH variable contains a .SAS extension. These are the possible
prefixes of the current executing program.
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;
If the condition above is true, then place the contents of the XPATH variable (the
program name) into a macro variable named &PGMNAME using the CALL SYMPUTX
call routine. The third argument of 'g' makes the macro variable &PGMNAME global.
Then stop the DATA step.
call symputx('pgmname',xpath,'g');
stop;
Example 6: How to Retrieve the Program Name That Is Currently Running in Batch Mode
or Interactively 457

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required