Post on 16-Apr-2017
BAS 150Lesson 8: SAS Macros
• Explain how a SAS Macro works
• Create a SAS Macro and SAS Macro Variable
• Incorporate a SAS Macro into an existing program
This Lesson’s Learning Objectives
SAS Macros (1 of 2) Most Important Concept to remember:
“You are writing a program that writes a program!”
Macros help you in many ways:o They allow you to write one section of code and use it over and over
again.
o You can make one small change in your code and have SAS make that change throughout your program.
o You can make your program similar to an algorithm, letting SAS decide what to do with the actual data.
The macro is a tool for simplifying and automating repetitive tasks
SAS Macros (2 of 2)Macros vs. Macro Variables:
• SAS Macro
o Often contains a Macro variableo Larger piece of programo Can contain complex logic
• SAS Macro Variable
o Standard data variable – does not belong to a data seto Only a single value, typically a character valueo Value of a Macro variable could be a name, numerical or text
The macro language is the syntax you use to create and use
macros
A macro variable is an efficient way of replacing text strings in
SAS code
o &name
A macro is a predefined routine you can use in a SAS program
o %name
What are macros?
%LET is a macro statement that creates a macro variable and
assigns it a value
Useful for simplifying code and automating reports
Macro variable can be used anywhere in SAS code, such as:
o Titles/Footnotes
o IF/WHERE statements
o Filenames
o Almost anywhere you have text capabilities
%LET (1 of 3)
%LET macro-variable = <value>;
macro-variable : name of new macro variable
value : character string or text expression that the macro
variable will represent
Resolve the macro variable later in your code with an
ampersand: ¯o-variable
%LET (2 of 3)
Report run every day for a product you choose
Each time you have to change the TITLE statement
and the date parameters in the WHERE statement
%LET (3 of 3)
Reference a macro variable with an ampersand preceding its name
If within a literal string (such as a title statement), enclose the string in double
quotation marks
o Macro variable references in single quotation marks will not resolve
Referencing Macro Variables (1 of 7)
Macro variable references can be placed next to leading or
trailing text
Output = “Sales as of Wednesday October 5 2016”
Referencing Macro Variables (2 of 7)
%LET product_name = ‘Orange';
%LET type_name = ‘Navel';
proc print data = store1011.produce;
where product = &product_name and type = &type_name ;
TITLE "Sales as of &SYSDAY &SYSDATE"; run;
The period delimiter will resolve with the macro variable reference
You may need a second period as part of the original text
HDD Report &Year..pdf resolves to HDD Report 2012.pdf
o The first period is the delimiter for the macro reference, and the
second is part of the text
Referencing Macro Variables (3 of 7)
Macro variable references can also be placed
next to each other
Dataset name HDD&Month&Year will resolve
to HDDApril2012
Referencing Macro Variables (4 of 7)
Use %put to see the resolved macro reference
in your log
oCan be useful for quickly de-bugging macro
references
Resolving Macro Variables (5 of 7)
Use the symbolgen option to display the resolution of macro
variable references in the log at the time they are executed
o Can also be useful for de-bugging
Resolving Macro Variables (6 of 7)
Macro variables are constant text strings
Even if it looks like an equation, it will be
treated like text
Resolving Macro Variables (7 of 7)
Use the %eval function to evaluate the
expression using integer arithmetic
%EVAL and %SYSEVALF (1 of 3)
%eval cannot be used with floating-point
numbers (numbers with a decimal point)
%EVAL and %SYSEVALF (2 of 3)
Use the %sysevalf function for floating-point
arithmetic
%EVAL and %SYSEVALF (3 of 3)
SAS has built in macro
variables called automatic
macro variables
o They are created at the
beginning of every SAS
session
o Use %put _automatic_;
to see the list in the log
Automatic Macro Variables (1 of 2)
Some potentially useful automatic macro variables:
o SYSDATE
o SYSDATE9
o SYSDAY
o SYSTIME
o SYSUSERID
Automatic Macro Variables (2 of 2)
How do you turn today’s date into a macro variable?
Option 1: Use the SYSDATE or SYSDATE9
automatic variables
o Problem – SYSDATE and SYSDATE9 are not dates, but
text strings. What if you don’t like that format?
Option 2: Use CALL SYMPUTX
CALL SYMPUTX (1 of 8)
CALL SYMPUT and CALL SYMPUTX assign
a value to a macro variable (similar to %LET)
Unlike %LET, the value can be based on a
calculation, algorithm or dataset variable
CALL SYMPUTX (2 of 8)
Below is an example of CALL SYMPUTX
The end result is macro variable that contains
today’s date with no special characters
oUseful for adding a date to filenames
CALL SYMPUTX (3 of 8)
DATA _NULL_
o Allows you to execute a DATA step without
creating a new dataset
CALL SYMPUTX (4 of 8)
TODAY()
oComputes the current date
o Example: March 26, 2013
CALL SYMPUTX (5 of 8)
PUT(today(), MMDDYY10.)
oConverts today’s date into a character string
o The string will have the appearance of the
MMDDYY10. date format
o Example: 03/26/2013
CALL SYMPUTX (6 of 8)
COMPRESS(put(today(), mmddyy10.), ‘/’)
oRemoves the forward slashes from the text string
o Example: 03262013
CALL SYMPUTX (7 of 8)
CALL SYMPUTX(‘DATE’,
compress(put(today(), mmddyy10.), ‘/’))
o Assigns this value to a macro variable called
DATE
o Similar to %LET date = 03262013;
CALL SYMPUTX (8 of 8)
Example: Have the DATA step store the SYSUSERID and
SYSDATE to keep record of who last modified the dataset
SYMGET Function (1 of 2)
The SYMGET function, by default, stores the
new variables as character variables with a
length of 200
SYMGET Function (2 of 2)
Anytime you find yourself repeating tasks or programs, you
might want to consider creating a macro
Macros are simply a group of SAS statements with a name
Instead of re-typing the statements, you use the macro
name to invoke the code
The invoked macro will write the code to your program
o Think of it as an advanced version of find/replace
Macro Programs (1 of 2)
%MACRO macro-name <(parameters)>;
macro-text
%MEND macro-name;
macro-name : name of new macro program
parameters : local macro variables, whose values you specify when you
invoke the macro
Resolve the macro variable later in your code with a percent sign:
%macro-name <(parameters)>
Macro Programs (2 of 2)
Writing a Macro
Invoking a macro
Use parameters to increase the flexibility of your macro
Similar to %LET
Parameters
Macro parameters are local macro variables
o They are defined in macro code
o They can only be used within the macro that defines them
Other macro variables are global macro variables
o They are defined in open code (non-macro code)
o They can be used anywhere in open code and in macro code
o Examples include automatic macro variables, variables created with %LET
Best practice: Do not create a local and global macro variable with the same name
Global vs Local
%IF, %THEN, %ELSE is similar to IF, THEN, ELSE,
but they are not the exactly same
o IF, THEN, ELSE conditionally executes SAS
statements in the DATA step
o %IF, %THEN, %ELSE conditionally generates text in a
macro
%IF, %THEN, %ELSE (1 of 2)
%IF, %THEN, %ELSE (2 of 2)
• Explain how a SAS Macro works
• Create a SAS Macro and SAS Macro Variable
• Incorporate a SAS Macro into an existing program
Summary - Learning Objectives
“This workforce solution was funded by a grant awarded by the U.S. Department of Labor’s
Employment and Training Administration. The solution was created by the grantee and does not
necessarily reflect the official position of the U.S. Department of Labor. The Department of Labor
makes no guarantees, warranties, or assurances of any kind, express or implied, with respect to such
information, including any information on linked sites and including, but not limited to, accuracy of the
information or its completeness, timeliness, usefulness, adequacy, continued availability, or
ownership.”
Except where otherwise stated, this work by Wake Technical Community College Building Capacity in
Business Analytics, a Department of Labor, TAACCCT funded project, is licensed under the Creative
Commons Attribution 4.0 International License. To view a copy of this license, visit
http://creativecommons.org/licenses/by/4.0/
Copyright Information