Storing Macro Definitions in External Files

The %INCLUDE Statement

One way to store macro programs permanently is to save them to an external file. You can then use the %INCLUDE statement to insert the statements that are stored in the external file into a program. If the external file contains a macro definition, the macro is compiled when the %INCLUDE statement is submitted. Then the macro can be called again later in the same program, or anytime later in the current SAS session.
Note: %INCLUDE is a global SAS statement, not a macro statement.
Syntax, %INCLUDE statement:
%INCLUDE file-specification </SOURCE2>;
file-specification
describes the location of the file that contains the SAS code to be inserted.
SOURCE2
causes the SAS statements that are inserted into the program to be displayed in the SAS log. If SOURCE2 is not specified in the %INCLUDE statement, then the setting of the SAS system option SOURCE2 controls whether the inserted code is displayed.
By storing your macro program externally and using the %INCLUDE statement, you gain several advantages over using session compiled macros.
  • The source code for the macro definition does not need to be part of your program.
  • A single copy of a macro definition can be shared by many programs.
  • Macro definitions in external files are easily viewed and edited with any text editor.
  • No special SAS system options are required in order to access a macro definition that is stored in an external file.

Example: Using the %INCLUDE Statement

You can compile a macro by using the %INCLUDE statement to insert its definition into a program. Then you can call the macro in order to execute it.
Suppose the following macro definition is stored in the external file C:Userscertadvprtlast.sas:
%macro prtlast;
   %if &syslast ne _NULL_ %then %do;
      proc print data=&syslast (obs=5);
         title "Listing of &syslast data set";
      run;
   %end;
   %else
      %put No data set has been created yet.;
%mend;
You could submit the following code to access, compile, and execute the Prtlast macro. The PROC SORT step is included in this example in order to create a data set that the Prtlast macro can print.
%include 'C:Userscertadvprtlast.sas' /source2;
proc sort data=certadv.courses out=work.bydays;
   by days;
run;
%prtlast
Note: The location and names of external files are specific to your operating environment.
The following messages are written to the SAS log when this code is submitted. Notice that the macro definition is written to the log because SOURCE2 was specified in the %INCLUDE statement.
Log 10.1 SAS Log
NOTE: %INCLUDE (level 1) file prtlast.sas is file
              C:Userscertadvprtlast.sas.
31   +%macro prtlast;
32   +   %if &syslast ne _NULL_ %then %do;
33   +      proc print data=&syslast(obs=5);
34   +      title “Listing of &syslast data set”;
35   +      run;
36   +   %end;
37   +   %else
38   +      %put No data set has been created yet.;
39   +%mend;
NOTE: %INCLUDE (level 1) ending.
40
41   proc sort data=certadv.courses out=work.bydays;
42      by days;
43   run;

NOTE: There were 6 observations read from the dataset
      CERTADV.COURSES.
NOTE: The data set WORK.BYDAYS has 6 observations and
      4 variables.
NOTE: PROCEDURE SORT used:
      real time           0.04 seconds
      cpu time            0.04 seconds

44
45   %prtlast
NOTE: There were 5 observations read from the dataset
      WORK.BYDAYS.
NOTE: PROCEDURE PRINT used:
      real time           1.07 seconds
      cpu time            0.26 seconds
Output 10.1 PROC PRINT Result of Work.Bydays
PROC PRINT Result of Work.Bydays
Last updated: October 16, 2019
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset