0% found this document useful (0 votes)
111 views19 pages

SAS Macro

The document introduces macros in SAS. It discusses that the macro facility contains a macro processor that translates macro code into statements for the SAS system. The macro language provides tools to pass information between steps, dynamically generate code, and conditionally execute steps. Macros allow for code reusability and flexibility. Key aspects of the macro language covered include defining macro variables, referring to them, local vs. global variables, defining and calling macros, and displaying variable values. More advanced topics discussed are macro parameters, built-in functions like %EVAL, and conditional logic using %IF/%THEN statements.

Uploaded by

ssr100@gmail
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views19 pages

SAS Macro

The document introduces macros in SAS. It discusses that the macro facility contains a macro processor that translates macro code into statements for the SAS system. The macro language provides tools to pass information between steps, dynamically generate code, and conditionally execute steps. Macros allow for code reusability and flexibility. Key aspects of the macro language covered include defining macro variables, referring to them, local vs. global variables, defining and calling macros, and displaying variable values. More advanced topics discussed are macro parameters, built-in functions like %EVAL, and conditional logic using %IF/%THEN statements.

Uploaded by

ssr100@gmail
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

MACRO LANGUAGE

1
INTRODUCTION TO MACROS

Macro Facility -

 The SAS macro facility is a tool within Base SAS


software that contains the essential elements that
enable us to use macros.

 The macro facility contains a macro processor


that translates macro code into statements that is
used by the SAS system, and the macro language.

2
INTRODUCTION TO MACROS

Macro Facility -

 Macro language provides the means to communicate


with the macro processor.

 Macro language consists of its own set of commands,


options, syntax and compiler.

3
INTRODUCTION TO MACROS

Macro Language Tools -

Macro language provides tools that

 Pass information between SAS steps.

 Dynamically create code at execution time.

 Conditionally execute DATA or PROC steps.

 Create generalized and flexible code.

4
INTRODUCTION TO MACROS

ADVANTAGES OF MACROS -

 Reducing the programming length/Size.

 Dynamically create code at execution time.

 Reducing the programming complexity.

 Create generalized and flexible code.


 Reuseability
5
INTRODUCTION TO MACROS

Defining Macro Variables

 Macro variables are also known as symbolic


variables.

 Macro variables are not dataset variables but they


belong to SAS macro language.

 Once a macro variable is defined it can take any


value during the execution of a SAS program.

6
INTRODUCTION TO MACROS

Defining Macro Variables -

A macro variable is defined by the %Let statement.


Syntax:-

%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

Referring To Macro Variables -

A macro variable defined inside or outside the


macro is referred by prefixing an ampersand (&)
before its name.

Example:-

%Let dname=Sample;
%Macro contents;
Proc Contents Data=&dname;
Run;
%Mend;
8
INTRODUCTION TO MACROS

Types Of Macro Variables -

There are two types of User-defined macro variables.


Local
Global

Local :- Variables defined inside the macro definition are


considered to be local variables.

Global :- Variables defined outside the macro definition,


or inside the macro specified as global, are considered to
be global variables.

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

Displaying Macro Variables -

The %Put statement writes text and the


current values of macro variables to the SAS
system LOG.

Syntax :-

%Put &Macro-Variable-list;
Example:-
%Let dname=Sample;
%Put *** select dataset is &dname;
14
ADVANCED MACRO LANGUAGE

Macro Parameters-

Macro parameters allow you to define


macro variables without using the %Let
statement. These values can be passed into a
macro when it is called.

Macro parameters are used to pass values


or text strings into a macro.

15
ADVANCED MACRO LANGUAGE

Simple Macro Function -

%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 -

By adding macro statements such as %If ,the


flexibility of macros can be increased.
The general form of the statements used for
conditional logic in macros :-
 %If condition %Then action;
%Else %If condition %Then action;
%Else action;

 %If condition %Then %Do;


SAS statements;
%End;
17
ADVANCED MACRO LANGUAGE

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

You might also like