Many times the macro variables created within a program become very long. The way these macro variables are stored can affect the efficiency of a program. One technique used to store fewer copies of the long macro variable, is the use of 3 ampersands through indirect referencing.
In the sample code you will notice two versions. The first version is the common way of passing a macro variable's value to a macro. In this first version there are 2 copies of the macro variable stored, PGM and VAL. Since &PGM is passed to the macro, &VAL must contain the value as well.
The second version of the code is a more efficient way of passing the long value to a macro. In this version, PGM is passed instead of &PGM so, &VAL only holds the string PGM. In the code, we use 3 ampersands by means of indirect referencing, to retrieve the value. The resolution of the ampersands are as follows:
&&&val && resolves to & &val resolves to pgm leaving &pgm which then resolves down to the long value
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
/* A long macro variable is created. */
%let pgm=%str(data exercise;
cardio=10;
weights=20;
totexer=cardio+weights;
proc print;
run;);
/* Generally when wanting to pass a macro variable to a macro definition
one would say */
options mprint symbolgen;
%macro check2(val);
&val /* Notice 1 ampersand here */
%mend check2;
%check2(&pgm) /* Notice the ampersand, causes &val to contain whole value */
/* The code below writes the same program as above, more efficiently */
options mprint symbolgen;
%macro check2(val);
&&&val /* Notice 3 ampersands here */
%mend check2;
%check2(pgm) /* Notice no ampersand, causes &val to contain only the string pgm */
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
/** Results from log of 1st version **/ /** Notice &VAL has the same long value of &PGM **/ SYMBOLGEN: Macro variable PGM resolves to data exercise; cardio=10; weights=20; totexer=cardio+weights; proc print; run; SYMBOLGEN: Some characters in the above value which were subject to macro quoting have been unquoted for printing. SYMBOLGEN: Macro variable VAL resolves to data exercise; cardio=10; weights=20; totexer=cardio+weights; proc print; run; SYMBOLGEN: Some characters in the above value which were subject to macro quoting have been unquoted for printing. /** Results from log of 2nd version **/ /** Notice &VAL only has a value of pgm **/ SYMBOLGEN: && resolves to &. SYMBOLGEN: Macro variable VAL resolves to pgm SYMBOLGEN: Macro variable PGM resolves to data exercise; cardio=10; weights=20; totexer=cardio+weights; proc print; run; SYMBOLGEN: Some characters in the above value which were subject to macro quoting have been unquoted for printing.
Type: | Sample |
Date Modified: | 2011-01-17 13:43:56 |
Date Created: | 2011-01-17 10:03:28 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | z/OS | ||
Z64 | ||||
OpenVMS VAX | ||||
Microsoft® Windows® for 64-Bit Itanium-based Systems | ||||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | ||||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | ||||
Microsoft Windows XP 64-bit Edition | ||||
Microsoft® Windows® for x64 | ||||
OS/2 | ||||
Microsoft Windows 95/98 | ||||
Microsoft Windows 2000 Advanced Server | ||||
Microsoft Windows 2000 Datacenter Server | ||||
Microsoft Windows 2000 Server | ||||
Microsoft Windows 2000 Professional | ||||
Microsoft Windows NT Workstation | ||||
Microsoft Windows Server 2003 Datacenter Edition | ||||
Microsoft Windows Server 2003 Enterprise Edition | ||||
Microsoft Windows Server 2003 Standard Edition | ||||
Microsoft Windows Server 2003 for x64 | ||||
Microsoft Windows Server 2008 | ||||
Microsoft Windows Server 2008 for x64 | ||||
Microsoft Windows XP Professional | ||||
Windows 7 Enterprise 32 bit | ||||
Windows 7 Enterprise x64 | ||||
Windows 7 Home Premium 32 bit | ||||
Windows 7 Home Premium x64 | ||||
Windows 7 Professional 32 bit | ||||
Windows 7 Professional x64 | ||||
Windows 7 Ultimate 32 bit | ||||
Windows 7 Ultimate x64 | ||||
Windows Millennium Edition (Me) | ||||
Windows Vista | ||||
Windows Vista for x64 | ||||
64-bit Enabled AIX | ||||
64-bit Enabled HP-UX | ||||
64-bit Enabled Solaris | ||||
ABI+ for Intel Architecture | ||||
AIX | ||||
HP-UX | ||||
HP-UX IPF | ||||
IRIX | ||||
Linux | ||||
Linux for x64 | ||||
Linux on Itanium | ||||
OpenVMS Alpha | ||||
OpenVMS on HP Integrity | ||||
Solaris | ||||
Solaris for x64 | ||||
Tru64 UNIX |