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

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

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: autoexec.sas;
filename Includes ('.'
                   'c:\SAS-site\includes'
                  );
 
* name: DemoMultipleIncludes.sas;
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\filea.sas.
7         +%Put Note2: C:\SAS-site\includes\FileA.sas;
Note2: C:\SAS-site\includes\FileA.sas
 
NOTE: %INCLUDE (level 1) ending.
NOTE: %INCLUDE (level 1) file INCLUDES(FileB) 
is file C:\Temp\sas\fileb.sas.
8         +%Put Note2: C:\Temp\sas\FileB.sas;
Note2: C:\Temp\sas\FileB.sas
 
NOTE: %INCLUDE (level 1) ending.

List Processing Example

The subroutine:

*name: ProcPrint.sas;
PROC Print data = &Libname..&Memname.;
           title2 &Libname..&Memname.;
           *title3 "%unquote(&MemLabel.)";
run;

Making a list using Proc Contents

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

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';
run;

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;
         end;
      Stmnt = cat('%Include Project(ProcPrint);');
      link ExecStmnt;
   end;
stop;
return; ExecStmnt:
   if   Testing then putlog    Stmnt=;
   call execute(cats('%nrstr(',Stmnt,')'));
return;
run;    %*calls executed in this step;
 
run;

References

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