Macros – SAS

Macros – SAS

Macros in SAS are a powerful way to automate the tasks that you need to perform every day. They make your tasks easier by allowing you to reuse your code multiple times after defining it once. Macros allow you to define dynamic variables in the code that can take different values for different run instances of the same code.

Macros in SAS are a powerful way to automate the tasks that you need to perform every day. They make your tasks easier by allowing you to reuse your code multiple times after defining it once. Macros allow you to define dynamic variables in the code that can take different values for different run instances of the same code.

For example, consider, you need to run the sales report every day and display the current date in the title of the code. Without a macro, you need to type the same commands each day to generate the report and provide the current date in the title of the report each day you run the report, as shown in the code extract below:

proc print data=Sales.Itemsold;

Where item=’chairs;

title “Item Sales On Friday 2Feb18

”;

Run;

You can automate the same code by using the dynamic variable for the current date and run the same code again each day without changing any line of code, as shown in the code extract below:

Proc Print Data= Sales.Itemsold;

Where item=’chairs;

Title “Item Sales on &SYSDAY &SYSDATE

”;

Run;

Global and Local Macro Variables

In the above given code, the SYSDATE and SYSDAY are global macro variables, which show system day and system date. Macros can have global variables and local variables. The global macro variables are system assigned variableswhich can be accessed by multiple SAS programs and local macro variables are accesses only by the programs in which they are defined.

The local macro variables are declared with %LET statement followed by a SAS variable name, which can be assigned a value. You can use the following syntax to declare a local macro variable:

% LET = Value;

Macro Programs

Macro programs are a set of SAS statements, which need to be run together. You can save these statements with a name. To start a macro program, you need to use %MACRO statement and to end the program, you need to use the %MEND statement.