Professional Documents
Culture Documents
Day5 PPT
Day5 PPT
Sandesh Sawant
Macro
• Used to reduce the amount of code entered to perform common tasks
• Used for tasks that are repeated in number of programs or number of different places within a
program.
• The text substitution produced by the macro processor is completed before the program text is
compiled and executed
• Macro language elements can only trigger text substitution and are not present during program
or command execution
Defining Macro
• Should always start with a %macro statement and end with a %mend statement
Syntax
%MACRO macro-name;
macro definition
%MEND macro-name;
where,
• macro-name is the name of the macro. Each macro should have a distinct name, which is subject
to the standard SAS naming conventions.
%MACRO print;
PROC PRINT DATA = ipl.bowlers;
RUN;
%MEND;
• Defining a macro called print for printing the data set bowlers.
Calling a Macro
Syntax
%macro-name;
where,
Example :- %print;
• %print will call the previously defined macro and print the dataset candy.
Example
%print;
• Macro parameters are local macro variables whose values are specified while invoking the
macro
• Macro variables defined in parentheses in %MACRO statement
• Two types
• Positional Parameters
• Keyword Parameters
Syntax
where,
• var1, var2 - macro variables – stores the values of the parameters passed.Refer to a macro
variable as &name in the macro definition. The form &name is called a macro variable reference.
Keyword Parameters
• can specify default values after the equal signs or specify the macro variable name followed by
an equal sign and the value in the macro invocation
Syntax
where,
Gambhir BT
%players(innings=1);
Dhawan BT
Karthik BT
Positional Parameters
• Names one or more macro parameters whose values will be specified while macro invocation
• Specify the values in the same order its defined in the macro definition
Syntax
where,
%macro players(plname,cat);
proc print data=ipl.players noobs;
name category
where name=&plname and category=&cat;
tilte "&plname is a &cat"; Bhatia BW
run;
%mend players;
%players("Bhatia“,"BW");
Macro Variables
• Are tools that enable you to dynamically modify the text in a SAS program through symbolic
substitution
• Use the variable by referencing it with an ampersand preceding its name (&variable-name),
anywhere in a SAS program
• Macro variable references that are enclosed in single quotation marks are not resolved
• Using a let statement inside a macro definition creates a local macro variable
• Using a let statement outside a macro definition creates a global macro variable
Syntax
where,
• <variable-name> is a name for the macro variable, satisfying SAS rules
• <variable-value> is the value to be assigned to the macro variable
Example:
Syntax
%gobal <variable-name>;
where,
• <variable-name> is a name for the macro variable, satisfying SAS rules
• Can be declared within the sas macro using let statement or using the keyword local
• Exists till the execution of that macro in which the variable is created.
Syntax
%local <variable-name>;
where,
• <variable-name> is a name for the macro variable, satisfying SAS rules
Example
%local player;
Defining Arithmetic and Logical Expressions
• can use arithmetic and logical expressions in specific macro functions and statements
%let a=%eval(1+2);
%let b=%eval(10*3);
%let a=%sysevalf(10.0*3.0);
%let b=%sysevalf(10.5+20.8);
%macro whatstep(info=,mydata=);
%if &info=print %then
%do;
proc print data=&mydata;
run;
%end;
%else %if &info=report %then
%do;
proc report data=&mydata nowd;
column name category;
title "DLF IPL 2008";
run;
%end;
%mend whatstep;
Syntax
where,
• macro-variable can be a character string that is a SAS name, enclosed in quotation marks or
name of a character variable whose values are SAS names
• data team1;
• input position : $8. player : $12.;
• call symput(position,player);
• datalines;
• shortstp Ann
• pitcher Tom
• frstbase Bill
• ;
Example
data team1;
input position : $8. player : $12.;
call symput(position,player);
datalines;
shortstp Ann
pitcher Tom
frstbase Bill
;
run;
Automatic Macro Variable
• Macro processor creates automatic macro variables that supply information related to the
SAS session when SAS is invoked
Example
SYSDAY
SYSDATE
SYSMACRONAME
User-Defined Macro Variable
Example
Syntax
%put ¯o-variable;
Example
%put &player;
%put ∑
Including external macros
%include “c:\files\name.sas”;
%players
%players (print=0)