e-Guide (Mini-book) |
Mind Map |
Getting Started |
Training Videos |
SAS® Savvy Class: Automating Tasks using SAS Macro and Metadata Programming |
As SAS® programmers advance in their career, there is at least an 80% chance that you will encounter SAS macros to help automate and standardize processes. SAS Macro programming are also known as 'Code Generators'. From SAS® macro essentials to advanced SAS® macro quoting functions, I am confident you will find these SAS® papers useful as they have been for me. See also Proc SQL, SAS Dictionary Tables and System Options. See SAS Savvy training sessions on SAS Macro Programming e-guide below.
In general, SAS® Macro Programs have three sections
A. Define Input Parameters with Defaults (Use global macro variables and system functions)
B. Data Process within Macro or Macro Do Loop (Use metadata and collect or report findings)
SAS® Guide SAS Macro Examples
SAS® Blog The SAS Training Post - Macro Q Functions, Author Tip, Environment, Parameters
Beginner SAS Programmer |
Beginner 1 Nine Steps to Get Started using SAS Macros, Jane Stroupe
Beginner 2 SAS Macros Are Just Text Substitution!!, Dianne Piaskoski
Beginner 3 Macro Basics for New SAS® Users, Cynthia L. Zender
Beginner 4 An Introduction to SAS® Macro, Ian Whitlock
Beginner 5 Macros Made Simple, Tom Mannigel
Beginner 6 Writing cleaner and more powerful SAS code using macros, Patrick Breheny [Presentation]
Hands-on-Workshops (HOW) SAS® Macro Programming for Beginners, Susan Slaughter, Lora Delwiche
(HOW) SAS® Macro Variables and Simple Macro Programs, Steven First, Katie Ronk
(HOW) Hands-On SAS® Macro Programming Tips and Techniques, Kirk Lafler [MRECALL]
Animated Guides by Russ Lavery: SAS Macro Programming, How %Str and %NRStr Work
Advanced SAS Programmer |
Advanced 1 CALL EXECUTE: How and Why, Ian Whitlock
Clinical Application Paper Considerations for Building an Integrated Safety Database Using SAS, Denise J. Smith, Daniel Schulz, Gayle Kloss, Wei Cheng
Clinical Application Paper Clinical Data Management: Building a Dynamic Application, Arthur Carpenter, Richard Smith
Macro SAS Programmer |
More About SAS Macros Examples Example User Guide
The Ultimate Macro Variable Name Model
call ur&&&varnama&pnum
Resolve from right to left, so do not include '.' between macro variables or counter
1st resolve &pnum to 1 for parameter record number
2nd resolve && to &varnama1 to alt for root variable
3rd resolve to uralt which is a variable name for related to root variable
ur &&& varnam a 1
< constant for new macro variables >&&&< macro variable name > < a-z columns > < 1-20 records >
ur - prefix for unit range or any type of variable reference for ULN
varnam - variable name such as ALT, AST, BILI
a - first letter for the first column to z for up to 26 columns in parameter dataset, do not use numbers since will conflict with counter suffix
1 - first row for up to 20 records in parameter dataset, counter numbers are ideal for sequence when creating macro variables using proc sql into
Macro Parameter Checklist - See SAS Debugging for automatic macro variables
Object |
Exists? |
Values/Records? |
Total |
# Unique |
Unique List (DoLoop/Arrays) |
First/Last |
---|---|---|---|---|---|---|
Macro Parameter |
X |
X |
||||
Libname/Pathname |
X |
N/A |
||||
File (Convert to dataset) - Excel (VDTs, Control files) - RTF/Log (Timestamp) |
X |
X |
X |
X |
X |
X |
Dataset |
X |
X |
X |
X |
X |
X |
Variable - Character (Text Management - spaces, case-sensitive, special characters) - Numeric/Dates (Calculations) |
X |
X |
X |
X |
X |
X |
Inside-Out SAS Syntax (Code Examples) |
Inside-Out SAS Syntax (Options, Metadata) |
---|---|
%IF %THEN; %ELSE %IF %THEN; ; %DO %TO; <CODE> %END; End Result - TITLES; FOOTNOTES;
DATA; <> ; RUN;
PROC REPORT ... COLUMN ...; DEFINE ...; COMPUTE BLOCK; RUN; |
Decision points to construct SAS Syntax Loop through list of ... One / More Values / Variables / Options Valid SAS Syntax Within/By SAS Procedure Direct (&name) / Indirect (&&name&i, &&&name&i) Macro Variable Reference Proc SQL / Proc Contents Metadata (type, length, label, unique values, or nobs) Non-Duplicate / Duplicate records Data-driven macro variables (create using SYMPUT and access using SYMGET) Non-Macro / Macro SAS functions Character Management functions Parameter Checks in Design |
Indirect Macro Reference |
Macro Resolution SAS Paper, 2 |
---|---|
Macro variable equals another macro variable. %let dog = hound; |
'&' resolves to name of macro variable. '&&&' resolves to internal macro variable value. %put My &pet is a &&&pet; |
Root name and suffix number as one macro variable. %let dsn = animals; |
'&' resolves to name of root and suffix macro variable name. '&&' resolves to internal macro variable value. %put ‘** &dsn&n = ‘ &dsn&n ‘ ** &&dsn&n = ‘ &&dsn&n; |
Root name and suffix number as one macro variable. %let i = 1; %let Engine1=Make Model EngineSize; |
'&' resolves to name of root and suffix macro variable name. '&&' resolves to internal macro variable value. var &&&var&i. ; |
Root name and two suffix numbers as one macro variable. %let i = 1; |
'&' resolves to name of root and suffix macro variable name. '&&' resolves to internal macro variable value. var &&rss_&i._&j; |
The Validator: A Macro to Validate Parameters, Paper 2 [Defensive Programming]
OBJECT_EXIST: A Macro to Check if a specified Object Exists, Jim Johnson
Macro Cleanup Checklist
Temporary Objects |
Delete at end of macro |
Libnames |
X |
Filenames |
X |
Datasets |
X |
Global Macro Variables |
X |
Carpenter's SAS Macro Programs
Categories: Character Variables, Dataset/File Management, Date/Time Variables, Library Tools, Macro Variables, Numeric Variables, and Programming Technique