Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25

SAS Training

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.

• It is used to automatically generate


– SAS statements and commands
– write messages to the SAS log
– Accept input
– Create and change the values of macro variables

• Macro facility has two components


1. Macro Processor – A part of SAS system does the Macro processing
2. Macro Language – Syntax that communicates to Macro Processor
Two delimiters trigger macro processor activity:
• &name - macro variable.
– Replacing Text Strings Using Macro Variables
– The form &name is called a macro variable reference.
• %name - refers to a macro
– Generating SAS Code Using Macros
– The form %name is called a macro call

• 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

• A SAS program can contain any number of macros

• can invoke a macro any number of times in a single program

• 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 definition can be any valid SAS step


Example

%MACRO print;
PROC PRINT DATA = ipl.bowlers;
RUN;
%MEND;

• Defining a macro called print for printing the data set bowlers.
Calling a Macro

• %name - refers to a macro

• The form %name is called a macro call.

Syntax

%macro-name;

where,

• macro-name is the name of the macro, which is to be called.

Example :- %print;

• %print will call the previously defined macro and print the dataset candy.
Example

%print;

Obs name category


1 Bhatia BW
2 Vettori BW
3 Maharoof BW
4 Geeves BW
5 McGrath BW
Passing Information into a Macro Using Parameters

• Macro parameters are local macro variables whose values are specified while invoking the
macro
• Macro variables defined in parentheses in %MACRO statement

• Enable to pass information into a 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

• names one or more macro parameters followed by equal signs

• 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,

• var1, var2 - macro parameters for holding the values passed.


• val1 is the default value to be stored in the parameter, to be used if no value is passed
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;
name category

Gambhir BT
%players(innings=1);
Dhawan BT

The names of the players for match 2 and innings 1

%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

• If no value is specified while invocation, null value will be taken

Syntax

%macro macro-name (var1,var2 ,….varn );


macro definition
%mend macro-name;

where,

• var1, var2 - macro parameters for holding the values passed.


Example

%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

• Can assign large or small amounts of text to macro variables

• Maximum length of 65,534 characters

• Are independent of SAS data set variables

• 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

• Classified into two based on the scope of the variable


• Global Macro Variable
• Local Macro Variable
• Can create a macro variable by using a let statement

• 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,
• <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:

%let playername = Tendulkar ;

%let sum = (100 + 400) ;


Global Macro Variables

• Exist till the duration of the SAS session

• Can be used within or outside a Macro

• Created during anytime of the SAS session

• Can assign a value using a let statement later

Syntax

%gobal <variable-name>;

where,
• <variable-name> is a name for the macro variable, satisfying SAS rules

Example: %global playername;


Local Macro Variables

• 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.

• It can be accessed/changed within the creation of macro.

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

• Macro Language Elements that Evaluate Arithmetic and Logical Expressions


• %DOmacro-variable=expression %TO expression<%BY expression>;
• %DO %UNTIL(expression);
• %DO %WHILE(expression);
• %EVAL (expression);
• %IF expression %THEN statement;
• %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;

%whatstep (info=report, mydata=player)


SYMPUT Routine

• Assigns a value produced in a DATA step to a macro variable

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;
Automatic Macro Variable

• Macro processor creates automatic macro variables that supply information related to the
SAS session when SAS is invoked

• Use %PUT _AUTOMATIC_ to view all available automatic macro variables

Example

SYSDAY
SYSDATE
SYSMACRONAME
User-Defined Macro Variable

• Macro variables created by the user

• Use %PUT _USER_ to view all available user-defined macro variables

Example

%let city = Bangalore;


%let player = Tendulkar;
%let sum = %eval(100 + 400);

• City, player and sum are macro variables


Displaying Macro Variable Values

• Use the %PUT statement to display macro variable values

• Values will be displayed in log window

Syntax

%put &macro-variable;

Example

%put &player;

%put &sum;
Including external macros

• Macros can be stored in external file can be used again

%include “c:\files\name.sas”;

%players

%players (print=0)

You might also like