SAS® Macro Programming    

Most Popular Links

 e-Guide (Mini-book)

Mind Map

 Getting Started

 Training Videos

  SAS® Savvy ClassAutomating 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.


SAS Macro Programming


 

 

______________________________________________

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)

C. Defensive Programming of Input Parameters and Data Processing (Display user message or abort if invalid dataset, variable name, data value or zero records)
                
Useful SAS Macro Debugging Options SYMBOLGEN (Confirm macro variable assignments) MPRINTNEST (Confirm resolved code executed) MLOGICNEST (Confirm correct program logic execution) 

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 ProgrammingHow %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

 

 

UCLA Self-Course

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 

       

 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;
%let pet = dog; 

'&' resolves to name of macro variable.  '&&&' resolves to internal macro variable value. 

%put My &pet is a &&&pet;
         My dog  is a hound;

Root name and suffix number as one macro variable.

%let dsn   = animals;
%let n      = 2;          
%let dsn2 = cats;           

'&' 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;
           **  &dsn&n =  animals2      **  &&dsn&n =  cats;

Root name and suffix number as one macro variable.

%let i = 1;
%let var=Engine;

%let Engine1=Make Model EngineSize;

'&' resolves to name of root and suffix macro variable name. '&&' resolves to internal macro variable value.

var &&&var&i. ;
var Make Model EngineSize;

Root name and two suffix numbers as one macro variable.

%let i = 1;
%let j = 3;
%let rss_1_3 = gender;

'&' resolves to name of root and suffix macro variable name. '&&' resolves to internal macro variable value.

var &&rss_&i._&j;
var gender;

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

 Filenames

X

 Datasets

 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