The Top 3 SAS Macros to Automate Your Tasks
As a data crunching and manipulation language, SAS provides you with a variety of data-centric, efficient procedures for dealing with large amounts of data.
SAS programs with repetitive code blocks tend to be long and code-heavy; Plus, manual scripting can be laborious and monotonous, increasing your margin for errors.
So, how can you automate code sections to perform repetitive tasks?
That’s where SAS macros come into play. Read on to learn what SAS macros are and how they improve SAS efficiency.
What are SAS macros?
Macros are variables that you can employ in SAS code to automate repetitive tasks. When using macros as coding elements, macro variables store dynamic character and numeric values, including names, letters, and numbers.
A macro code consists of two building blocks, the macro itself and the macro variables. In SAS programs you need to differentiate between them by using & and % signs.
For example, if you type &employee, you are calling a macro variable. But, if you type %employee, you are defining a macro variable.
types of sas macros
There are some built-in macros that you would commonly use in SAS. They will prove to be very valuable for real-time applications depending on the use-case.
In the output, values in the age column that are not equal to 10 are filtered out once the condition is met. The macro variable is defined next to the %let statement, which is used to equalize the values in the condition.
If you want to execute the values after the filter condition has changed, all you have to do is change the value from 10 to the desired number in the %let statement. It’s that simple; Rerun the code to see the new results.
2. Macro Parameters
Macro parameters are helpful when you want to define variables and steps directly within a set of macro statements. Entire blocks of statements can be reused to perform different tasks as and when needed.
For example, suppose you want to sort an existing dataset by some criteria.
In the first block of code, create a dataset with some fake values. Once the data is available, you can create a macro statement within the %macro and %mend statements.
In the %macro statement, you can define the name of the macro with a local variable that will hold some value when the macro is called.
3. Using proc sql as macro variable
SQL is one of the most commonly used languages within SAS for data manipulation and data processing. Having said that, it is imperative to be able to store values in SQL variables for further use. Macros and SQL are a strong combination, and using them together can go a long way.
For example, if you have a list of values that you want to store in a macro variable, you can use a Proc SQL statement to create the macro variable.
If you wish to do so you can use conditions to filter variable values using a where statement. Otherwise, with normal conditions, you can still save the value in a variable. This includes character and numeric values.
The output contains a list of all age values from the table test_example, each separated by a comma. This can be used to filter information in another Proc SQL statement when you are pulling information from server tables.
Using SAS Macros to Automate Repetitive Tasks
This list of macros is not exhaustive. There are some other variations that can make your life easier, especially if you code every day.
However, macros are not limited to SAS only. There are some macro variations available in VBA that can be used to automate daily repetitive tasks in Excel, Word and other MS Office applications.