Adv. SAS (Macro)
Adv. SAS (Macro)
Adv. SAS (Macro)
Macro
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.
Example
%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;
Obs
name
category
Bhatia
BW
Vettori
BW
Maharoof
BW
Geeves
BW
McGrath
BW
Macro parameters are local macro variables whose values are specified while invoking the
macro
Two types
Positional Parameters
Keyword Parameters
Syntax
%macro macro-name(var1 ,var2,.varn );
macro definition
%mend macro-name;
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
%macro macro-name (var1 = [val1] ,var2 = ,.varn = );
macro definition
%mend macro-name;
where,
Example
%macro players(match=1, innings=);
proc print data=ipl.players noobs;
var name type;
where match=&match and innings=&innings;
title The names of the players for match &match and innings &innings;
run;
The names of the players for match 1 and innings 1
%mend players;
%players(innings=1);
name
category
Gambhir
BT
Dhawan
BT
%players(innings=1,match=2);
name
category
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
%macro macro-name (var1,var2 ,.varn );
macro definition
%mend macro-name;
where,
Example
%macro players(plname,cat);
proc print data=ipl.players noobs;
where name=&plname and category=&cat;
tilte "&plname is a &cat";
run;
%mend players;
%players("Bhatia,"BW");
name
category
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
%let <variable-name> = <variable- value>;
where,
Example:
%let playername = Tendulkar ;
%let sum = (100 + 400) ;
Syntax
%gobal <variable-name>;
where,
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;
can use arithmetic and logical expressions in specific macro functions and statements
%DO %UNTIL(expression);
%DO %WHILE(expression);
%EVAL (expression);
%SCAN(argument,expression,<delimiters>)
%SUBSTR(argument,expression<,expression>)
%SYSEVALF(expression,conversion-type)
Example
%let a=%eval(1+2);
%let b=%eval(10*3);
%let a=%sysevalf(10.0*3.0);
%let b=%sysevalf(10.5+20.8);
%let address=123 maple avenue;
%let frstword=%scan(&address,1);
%let lincoln=Four score and seven;
%let secondwd=%substr(&lincoln,6,5);
Example
%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;
SYMPUT Routine
Syntax
CALL SYMPUT(macro-variable, value);
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
%let sr_cit = no;
data senior;
set census;
if age > 65 then
do;
call symput ("sr_cit", yes);
output;
end;
run;
data team1;
input position : $8. player : $12.;
call symput(position,player);
datalines;
shortstp Ann
pitcher Tom
frstbase Bill
;
run;
Macro processor creates automatic macro variables that supply information related to the SAS
session when SAS is invoked
Example
SYSDAY
SYSDATE
SYSMACRONAME
Example
%let city = Bangalore;
%let player = Tendulkar;
%let sum = %eval(100 + 400);
Syntax
%put ¯o-variable;
Example
%put &player;
%put ∑
%include c:\files\name.sas;
%players
%players (print=0)