0% found this document useful (0 votes)
53 views

Why Use Macros

Uploaded by

Pulkit Manchanda
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Why Use Macros

Uploaded by

Pulkit Manchanda
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 38

WHY USE

MACROS?
INTRODUCTION
 The SAS® Macro Language allows you
– to extend and customize the SAS System
– to simplify large programs
• to reduce the amount of text you enter
– to invoke code several times
– to minimize the mistakes you might make
 The macro language is a powerful addition
to SAS, allowing increased efficiency and
customization of SAS jobs

2
INTRODUCTION
 Macro programming is generally
considered an advanced topic
 But, while macros certainly can be
challenging, it is also true that the basic
concepts are not difficult to learn

3
INTRODUCTION
 This course is designed for students
who know the basics of SAS
programming, but know nothing about
SAS macro programming
 As we won’t start from the very
beginning, if you are not a SAS user at
all, then it is not a course for you

4
INTRODUCTION
 We explain
– how the macro processor works, and
– how to use macros and macro variables
 Using these techniques you can create
flexible, reusable code that can save
you time and effort

5
INTRODUCTION
 Because macro code takes longer to
write and debug than standard SAS
code, you generally won’t use macros
in programs that will be run only a few
times
 But if you find yourself writing similar
code over and over again, then macros
may make your job easier

6
MACROS CAN HELP
 First, with macros you can make one small
change in your program and have SAS echo
that change throughout your program
 Second, macros can allow you to write a
piece of code and use it over and over again
in the same program or in different programs
 Third, you can make your programs data
driven, letting SAS decide what to do based
on actual data values

7
THE MACRO PROCESSOR
 The most important concept to keep in
mind whenever you write macro code
is that

You are writing a program


that writes a program
macro macro standard
statements processor SAS statements

8
INTRODUCTION
 When you submit a standard SAS
program, SAS compiles and then
immediately executes it
 But when you write macrocode, there
is an extra step

9
INTRODUCTION
 Before SAS can compile and execute
your program, SAS must pass your
macro statements to the macro
processor which then “resolves” your
macros generating standard SAS code
 Because you are writing a program that
writes a program, this is sometimes
called meta-programming

10
Let’s do exercises
 Scenario
– 25 data sets with one same variable named
TEMP for year 1981 – 2006
• YR1981, YR1982, …. , and YR2006
– Find mean TEMP for each year

PROC MEANS DATA=yr1981;


VAR temp;
RUN;

11
INTRODUCTION
 The SAS macro language consists of
– macro variables,
– macro programs,
– macro facility interfaces, and
– macro storage techniques

12
MACRO VARIABLES
 SAS macro variables are the basic units that
are used by macro facility interfaces and
macro programs
 They can be created and resolved anywhere
in a SAS program
 The name and value of a macro variable is
stored in memory in a Symbol Table, either
local or global in scope; the value is always
simply a string of characters

13
MACRO VARIABLES
 Macro variables come in two varieties: either
local or global
 A macro variable’s scope is local if it is
defined inside a macro
 Its scope is global if it is defined in “open
code” which is everything outside a macro.
 You can use a global macro variable
anywhere in your program, but you can use a
local macro variable only inside its own
macro.

14
MACRO VARIABLES
 If you keep this in mind as you write
your programs, you will avoid two
common mistakes:
– trying to use a local macro variable
outside its own macro, and
– accidentally creating local and global
macro variables having the same name

15
MACROS vs. MACRO
VARIABLES
 A macro variable is like a standard
data variable except that it does not
belong to a data set and has only a
single value which is always character
 The value of a macro variable could be
a variable name, a numeral, or any text
you want substituted in your program

16
MACROS vs. MACRO
VARIABLES
 The names of macro variables start with an
ampersand (&), while the names of macros
start with a percent sign (%)
 The % and & characters are called macro
triggers; the character following the macro
trigger must not be a space

%let dset = MASTER ;


PROC PRINT DATA=&dset ;
RUN;

17
USER-DEFINED MACRO
VARIABLES
 the %LET statement enables you to define a
macro variable and assign it a value
• %LET variable = value ;
– variable can be any name following SAS naming
conventions
– value can be any string
• numeric tokens are stored as character strings,
• quotes are stored as part of the value
• the case of value is preserved
• numeric tokens are stored as character strings

18
USER-DEFINED MACRO
VARIABLES
 the %LET statement enables you to define a
macro variable and assign it a value
• %LET variable = value ;
– length range is 0-32K characters
– mathematical expressions are not evaluated
– leading and trailing blanks are removed from
value before the assignment is made
– if variable already exists, value replaces the
current value

19
SYSTEM-DEFINED MACRO
VARIABLES
 System-defined macro variables are
created by the SAS Supervisor
– created at SAS invocation
– are global(always available)
– can be assigned values by the user in
some cases

20
SYSTEM-DEFINED MACRO
VARIABLES
 some system-defined macro variables
NAME VALUE
SYSDATE date of SAS invocation

SYSDAY day of the week of SAS invocation

SYSTIME time of SAS invocation

SYSSCP operation system being used

SYSVER release of SAS system being used

SYSERR return code set by last DATA or PROC step

SYSLAST name of most recently created SAS data set in the form of
libref.name. If no data set was created then the value is
_NULL_ . 21
SYSTEM-DEFINED MACRO
VARIABLES
 NOTE: the value of macro variables
SYSDATE and SYSTIME are character
strings, not SAS date or time values.
 Example:
%let job = HW1 ;
Footnote "&job by YUFEN LI on
&SYSDATE &SYSTIME";
Footnote " HW1 by YUFEN LI on 06NOV06 18:45 "

22
MACRO PROGRAMS
 similar to a subroutine or function in a
procedural programming language e.g.
Fortran, C/C+/C++
 with a name which is used to call it,
and it can accept parameters
 the names of macros start with a
percent sign (%)

23
MACRO PROGRAMS
 Syntax
%MACRO macro-name (parameters);
… macro-text …
%MEND macro-name;

– parameters are separated by commas


– there are two type of macro parameters:
positional and keyword

24
TYPE OF MACRO
PARAMETERS
 positional parameters receive their values in the order
(position) in which they are specified in the macro
invocation
– positional parameters, if any, must appear first in the parameter list
 keyword parameters are those which start with the name
of the macro variable followed by an = symbol and
optionally followed by an initial value
– keyword parameters may appear in any order after positional
parameters
%macro means(procopt,vars,dsn=_last_) ;
proc means &procopt data = &dsn;
var &vars ;
%mend means;

25
MORE FLEXIBILITIES
 The following macro definition has one
positional parameter ‘opts’ (with no = sign)
and one keyword parameter ‘filenm’ (with an
= sign)

%macro mname (opts,filenm=file5);


proc print data=&filenm &opts;
title " Print &filenm data ";
run;
%mend;

26
MORE FLEXIBILITIES
 This macro can be called with any of
these formats, specifying either or both
or neither of the parameters:

%mname ()
%mname (double)
%mname (filenm=file6)
%mname (double noobs, filenm=file7)

27
MACRO PROGRAMS
 Example
%macro mname;
proc print data=data.all;
where var1=&newvalue;
title " Print &syslast data ";
run;
%mend;

OPTIONS MPRINT MLOGIC;


%let newvalue=17;
%mname

28
MACRO PROGRAMS
 Notice that there is NO SEMICOLON after
‘%mname’
 This line calls the macro named ‘mname’;
there is no semicolon there because that line
will be replaced by the code from the macro
program itself, which has the required
semicolons
 Specify the MPRINT and MLOGIC options to
generate as much information as possible
from the macro processor

29
OPTION MPRINT
 You can see that SAS has inserted into the
regular SAS log the MPRINT lines
 The statements generated by the macro
processor are all labeled with the word
MPRINT followed by the name of the macro
that generated the statements
 By using the MPRINT system option it is
easy to see the standard SAS statements
your macro is generating

30
OPTION MLOGIC
 Use MLOGIC to debug macros
 If MLOGIC is in effect and the macro processor
encounters a macro invocation, the macro
processor displays messages that identify
– the beginning of macro execution
– the values of macro parameters at that point
– the execution of each macro program statement
– whether each %IF condition is true or false
– each iteration of the %DO loop
– the end of macro execution

31
MACRO FACILITY
INTERFACES
 Macro Facility Interfaces are ways to
use macro variables. They are :
1) CALL SYMPUT (macro-variable, text);
This creates a macro variable named
‘macrovariable’ with a value of ‘text’.

32
MACRO FACILITY
INTERFACES
2) Indirect References using multiple
ampersands. A double ampersand (&&) in
SAS code is resolved to a single ampersand

If we have a macro variable ‘var1’ with a


value of ‘char1’, and another macro variable
‘char1’ with a value of ‘Taiwan’, then a string
in the SAS code of ‘&&&var1’ is resolved to
‘&char1’ on the first pass, and that is
resolved to ‘Taiwan’ on the second pass

33
MACRO FACILITY
INTERFACES
3) PROC SQL; SELECT … INTO
:macrovariable…;
proc sql;
select sum(amount) into
:total from data.overall;
run;
This defines a macro variable ‘total’ with a
value that is the string representing the sum of
variable ‘amount’ in SAS data set data.overall

34
STORAGE TECHNIQUES
There are three ways to store macro programs for
future use:
1. Store the source code in an external file and
use this statement to pull it into a SAS program:
%include(file-pathname);

2. Use the Autocall Facility to search predefined


source libraries for macro definitions. These
libraries can consist of external files or SAS
catalogs.

35
STORAGE TECHNIQUES
3. Store the compiled macros. This does NOT save the
SOURCE code. Always maintain the source code
separately
To store the compiled macro :
libname dlib ‘library-path’;
options mstored sasmstore=dlib;
%macro mname(parameters) / store
des=’description’;

%mend;
To access this stored macro :
libname dlib ‘library-path’;
options mstored sasmstore=dlib;
%mname(parameters)

36
In-Class Assignment
 Write a SAS macro program to provide the
solutions of aX2 + bX + c = 0 for varied a, b,
and c
 What expected to see in the OUTPUT is

Question Solution1 Solution2


X2 + 4X + 4 = 0 -2 -2
X2 + 5X + 4 = 0 -1 -4
X2 - 5X + 4 = 0 4 1

37
In-Class Assignment
1. Start with a general SAS code to print
out the solutions of X2 + 4X + 4 = 0
2. Convert the previous code as a
macro program with three parameters
to take values of a, b, and c
3. Celebrate with your first macro code

38

You might also like