As the first step in the decommissioning of the site has been converted to read-only mode.

Here are some tips for How to share your SAS knowledge with your professional network.

Parameterized include file

From sasCommunity
Jump to: navigation, search

Parameterized include file

A Parameterized include file is a program which uses global macro variables as its parameters.

It is an often over-looked intermediate step in development of macros.

  • Pro: no need for macro options
  • Con:
    • conditional execution of statements requires more expertise
    • no %do loops
    • no auto-search facility like option autocall for macros
    • no provision of defaults
  • Testing: options source2;

Basic Examples

filename Project '.'; * here: see also SASinitialFolder;
%Let Data = sashelp.class;
%Include Project(ProcPrint);
PROC Print data = &Data.;

Filename With Multiple Folders

In responses to a SAS-L thread about why do we use macros many answers referred to the macro option sasautos, which provides a list of folders for sas to search for files containing macros.

This programs shows how to allocate a fileref with multiple folders.

* name:;
filename Includes ('.'
* name:;
options source2;
%Include Includes(FileA FileB);
3          filename Includes ('.'
4                             'c:\SAS-site\includes' );
5          options source2;
6          %Include Includes(FileA FileB);
NOTE: %INCLUDE (level 1) file INCLUDES(FileA) 
is file c:\SAS-site\includes\
7         +%Put Note2: C:\SAS-site\includes\;
Note2: C:\SAS-site\includes\
NOTE: %INCLUDE (level 1) ending.
NOTE: %INCLUDE (level 1) file INCLUDES(FileB) 
is file C:\Temp\sas\
8         +%Put Note2: C:\Temp\sas\;
Note2: C:\Temp\sas\
NOTE: %INCLUDE (level 1) ending.

List Processing Example

The subroutine:

PROC Print data = &Libname..&Memname.;
           title2 &Libname..&Memname.;
           *title3 "%unquote(&MemLabel.)";

Making a list using Proc Contents

%Let Libname = SAShelp;
PROC Contents data   = &Libname.._all_
              out    = Work.ListMemNames
             (where  = (MemType eq 'DATA')
              keep   = Libname MemName MemLabel);
Proc Sort data = Work.ListMemNames
          out  = Work.ListMemNames
          by     Libname Memname;

Making a list using Proc SQL

%Let Libname = SAShelp;
PROC SQL noprint;
         create table Work.ListMemNames as
         select Libname, Memname, MemLabel
         from   Dictionary.Tables
         where  Libname eq "%upcase(&Libname.)"
           and  Memtype eq 'DATA';

A Parameterized Include Calling Program

%*Let ListData  =;
%*Let ListNames =;
DATA   _Null_;
attrib Stmnt length = $132
       Vname length = $ 32;
array  Mvar(*) $32 Libname MemName; 
*array  Mvar(*) $256 Libname MemName MemLabel;
*array  Mvar(*) $256 &ListNames.;
*array  Mvar(*) $256 _character_;
retain Testing %eval(0
            or %sysfunc(getoption(Source2))
                               eq SOURCE2  );
do until(EndoFile);
   * set &ListData. end = EndoFile;
   set Work.ListMemNames end = EndoFile;
      do I = 1 to dim(Mvar);
         call   vname(Mvar(I)    ,Vname);
         Stmnt = catx(' ','%let ',Vname,'='
                     ,Mvar(I)           ,';');
         link ExecStmnt;
      Stmnt = cat('%Include Project(ProcPrint);');
      link ExecStmnt;
return; ExecStmnt:
   if   Testing then putlog    Stmnt=;
   call execute(cats('%nrstr(',Stmnt,')'));
run;    %*calls executed in this step;


--macro maven == the radical programmer 10:46, 20 June 2007 (EDT)