SAS Macro
SAS Macro
1
INTRODUCTION TO MACROS
Macro Facility -
2
INTRODUCTION TO MACROS
Macro Facility -
3
INTRODUCTION TO MACROS
4
INTRODUCTION TO MACROS
ADVANTAGES OF MACROS -
6
INTRODUCTION TO MACROS
%Let Macro-var-name=value;
Example:-
%Let dname=Sample;
Note: - Quotation marks are not required. If quotation marks are used, they
become part of the value.
7
INTRODUCTION TO MACROS
Example:-
%Let dname=Sample;
%Macro contents;
Proc Contents Data=&dname;
Run;
%Mend;
8
INTRODUCTION TO MACROS
9
INTRODUCTION TO MACROS
Syntax :-
%Global Macro-Variable-list;
%Local Macro-Variable-list;
Example:-
%Macro in (var);
%Local SS;
%Let SS=&var;
%put inside &bb;
%Mend in;
%Let SS=5;
%in(3)
%put outside &SS;
10
INTRODUCTION TO MACROS`
Defining A Macro -
Any macro code should be with in the two
macro language statements %Macro and
%Mend.
Syntax : -
%Macro macro-name;
- - - - Macro - - text;
%Mend <macro-name>;
Note :- No macro name can begin with ‘sys’ or the name of a SAS
supplied macro statement or a macro function.
11
INTRODUCTION TO MACROS
Example
SAS Code
Proc Print Data=Sample;
Title ‘The Data Set Name Sample’;
Run;
Macro Code
%Macro Print;
Proc Print Data=&dname;
Title "The Data Set Name &dname";
Run;
%Mend Print;
Note: To resolve macro variable in a macro, use double quote ->
12
INTRODUCTION TO MACROS
Calling A Macro -
Macros are called by placing a (%) before the
macro name.
Example:-
%Macro Sort;
Proc Sort Data=&dname;
By &var1;
Run;
%Mend Sort;
%Let dname=First;
%Let var1=duns;
%Sort;
13
INTRODUCTION TO MACROS
Syntax :-
%Put &Macro-Variable-list;
Example:-
%Let dname=Sample;
%Put *** select dataset is &dname;
14
ADVANCED MACRO LANGUAGE
Macro Parameters-
15
ADVANCED MACRO LANGUAGE
%Eval
The evaluation functions evaluate arithmetic and logical
expressions. The following are examples of very basic
arithmetic and logical evaluation functions.
Example:-
%let a = 1;
%let b = &a + 1;
%let c = %Eval (&a + 1);
%Put a = &a, b = &b, and c = &c ;
In Log :a = 1 ,b = 1 + 1 and c = 2
16
ADVANCED MACRO LANGUAGE
Conditional Logic -
Example
%Macro Monthwise(month);
Data Monthwise;
Set %If month=jan %Then jan;
%Else %If month=feb %Then feb;
;
Run;
%Mend Monthwise;
%Monthwise(month);
18
19