WHY USE MACROS?
- With macros, you can make one small change in your program and have SAS echo that change throughout your program.
- It allows you to write a piece of code and use it over and over again in the same program or in different programs.
- you can make your program's data driven, letting SAS decide what to do based on actual data values.
When you submit a standard SAS program, SAS compiles and then immediately executes it. But when you write macro code, there is an extra step.
Before SAS can compile and execute your program, SAS must pass your macro statements to the macro processor which then “resolves” your macros generating standard SAS code. Because you are writing a program that writes a program, this is sometimes called meta-programming.
The names of macro variables start with an ampersand (&), while the names of macros start with a percent sign (%)
Using %LET
%LET macro statement simply assigns a value to a macro variable.
The general form a %LET statement is %LET macro-variable-name = value;
where macro-variable-name is a name of the macro variable
Value is the text to be substituted for the macro variable name,
Eg: %LET count = 10;
%LET blog = Padna Seekhna.
---------------------------------------
To use the macro variable, you simply add the ampersand prefix (&) and stick the macro variable name wherever you want its value to be substituted.
DO i = 1 to &count;
TITLE “BlogName: &blog”;
---------------------------------
After being resolved by the macro processor, these statements would become
DO i = 1 to 10;
TITLE “BlogName: Padna Seekhna”;
-------------------------------------------
Eg: A company that manufactures bicycles maintains a file listing all their models. For each model they record its name, class (Road, Track, or Mountain), list price, and frame material
This DATA step reads the raw data from a file named Models.dat creating a SAS data set named MODELS.
DATA models;
INFILE 'c:\Models.dat' TRUNCOVER;
INPUT Model $ 1-12 Class $ Price Frame $ 28-38;
RUN;
------------------------------------------------------------
%LET bikeclass = Mountain;
* Use a macro variable to subset;
PROC PRINT DATA = models NOOBS;
WHERE Class = "&bikeclass";
FORMAT Price DOLLAR6.;
TITLE "Current Models of &bikeclass Bicycles";
RUN;
---------------------------------------------------------------
CREATING MODULAR CODE WITH MACROS
The %MACRO statement tells SAS that this is the beginning of the macro and the %MEND statement signals the end of the macro.
The general form of a macro is:
%MACRO macro-name;
macro-text
%MEND macro-name;
------------------
Eg: %MACRO PadnaSeekhna;
PROC PRINT DATA = models NOOBS;
TITLE 'Current Models';
VAR Model Class Frame Price;
FORMAT Price DOLLAR6.;
RUN;
%MEND PadnaSeekhna;
----------------------------
ADDING PARAMETERS TO MACROS
To add parameters to your macro, simply list the macro-variable names followed by an equal sign in parentheses after the macro name:
%MACRO macro-name (parameter-1=, parameter-2=, . . . parameter-n=);
macro-text
%MEND macro-name;
--------------------------
MPRINT SYSTEM OPTION
This can be very useful for debugging purposes. To turn on the MPRINT option, submit an OPTIONS statement like this:
OPTIONS MPRINT;
-----------------------
CONDITIONAL LOGIC
You can increase that flexibility still more by using conditional macro statements such as %IF.
%IF condition %THEN action;
%ELSE
%IF condition %THEN action;
%ELSE action;
%IF condition %THEN %DO;
action;
%END;
-------------------------------------------
AUTOMATIC MACRO VARIABLES
Every time you invoke SAS, the macro processor automatically creates certain macro variables. You can use these in your programs.
Two of the most commonly used automatic variables are
Using %LET
%LET macro statement simply assigns a value to a macro variable.
The general form a %LET statement is %LET macro-variable-name = value;
where macro-variable-name is a name of the macro variable
Value is the text to be substituted for the macro variable name,
Eg: %LET count = 10;
%LET blog = Padna Seekhna.
---------------------------------------
To use the macro variable, you simply add the ampersand prefix (&) and stick the macro variable name wherever you want its value to be substituted.
DO i = 1 to &count;
TITLE “BlogName: &blog”;
---------------------------------
After being resolved by the macro processor, these statements would become
DO i = 1 to 10;
TITLE “BlogName: Padna Seekhna”;
-------------------------------------------
Eg: A company that manufactures bicycles maintains a file listing all their models. For each model they record its name, class (Road, Track, or Mountain), list price, and frame material
This DATA step reads the raw data from a file named Models.dat creating a SAS data set named MODELS.
DATA models;
INFILE 'c:\Models.dat' TRUNCOVER;
INPUT Model $ 1-12 Class $ Price Frame $ 28-38;
RUN;
------------------------------------------------------------
%LET bikeclass = Mountain;
* Use a macro variable to subset;
PROC PRINT DATA = models NOOBS;
WHERE Class = "&bikeclass";
FORMAT Price DOLLAR6.;
TITLE "Current Models of &bikeclass Bicycles";
RUN;
---------------------------------------------------------------
CREATING MODULAR CODE WITH MACROS
The %MACRO statement tells SAS that this is the beginning of the macro and the %MEND statement signals the end of the macro.
The general form of a macro is:
%MACRO macro-name;
macro-text
%MEND macro-name;
------------------
Eg: %MACRO PadnaSeekhna;
PROC PRINT DATA = models NOOBS;
TITLE 'Current Models';
VAR Model Class Frame Price;
FORMAT Price DOLLAR6.;
RUN;
%MEND PadnaSeekhna;
----------------------------
ADDING PARAMETERS TO MACROS
To add parameters to your macro, simply list the macro-variable names followed by an equal sign in parentheses after the macro name:
%MACRO macro-name (parameter-1=, parameter-2=, . . . parameter-n=);
macro-text
%MEND macro-name;
--------------------------
MPRINT SYSTEM OPTION
This can be very useful for debugging purposes. To turn on the MPRINT option, submit an OPTIONS statement like this:
OPTIONS MPRINT;
-----------------------
CONDITIONAL LOGIC
You can increase that flexibility still more by using conditional macro statements such as %IF.
%IF condition %THEN action;
%ELSE
%IF condition %THEN action;
%ELSE action;
%IF condition %THEN %DO;
action;
%END;
-------------------------------------------
AUTOMATIC MACRO VARIABLES
Every time you invoke SAS, the macro processor automatically creates certain macro variables. You can use these in your programs.
Two of the most commonly used automatic variables are
- &SYSDATE(20DEC16) - Character value of the date that job or session began ,
- &SYSDAY (Sunday) - Day of the week that job or session began
No comments:
Post a Comment