Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 37 ยท Macros

SAS Macro Variables

Learn SAS macro variables: %LET, resolving &name, the trailing period rule, double quotes vs single quotes, automatic variables, CALL SYMPUTX, SYMGET and scope, with a Python stand-in.

The SAS macro language is a text-substitution system that runs before your code does. It writes SAS code for you. Instead of retyping 2025 in twelve places, you store it once in a macro variable and refer to it by name. When you change the value, every reference changes with it.

SAS cannot run on this site; sas blocks are read-only. The Python TryIts imitate the macro processor's text substitution so you can see what SAS does before it compiles the code. They are a teaching model, not the real macro engine.

Creating a macro variable with %LET

SAS
%let year = 2025;
%let region = East;

proc print data=work.sales;
  where year = &year and region = "&region";
run;

%let name = value; stores text. There are no quotes and no types: everything is text, and leading and trailing blanks are trimmed. To use the value, write an ampersand before the name: &year. The macro processor replaces &year with 2025 and &region with East, and only then does SAS see the resulting code:

SAS
proc print data=work.sales;
  where year = 2025 and region = "East";
run;

Because macro values are plain text, %let year = 2025; does not create a number. SAS sees 2025 as digits only because they end up in numeric context.

Double quotes vs single quotes

The macro processor looks inside double quotes but not inside single quotes:

SAS
title "Sales for &region";     /* resolves: Sales for East */
title 'Sales for &region';     /* literal: Sales for &region */

This is the most common beginner bug in a macro-enabled program. Whenever a macro variable appears inside a string, use double quotes.

Stand-in: resolving & references

Python Stand-in: a tiny macro processor

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The second example already shows the period rule that comes next: sales&year becomes sales2025, and &year..csv becomes 2025.csv.

The period rule

A macro variable name ends at the first character that cannot be part of a name. Sometimes you want text glued to the value. A period tells SAS where the name stops, and the period is consumed:

SAS
%let lib = sales;
data &lib..report;          /* becomes: data sales.report; */
  set &lib..raw;
run;

%let yr = 25;
data results&yr.q1;         /* becomes: results25q1 */
run;

For a library reference, &lib..report needs two periods: the first ends the name, and the second is the real dot of library.dataset. Forgetting this is a classic mistake, because one period silently disappears.

Automatic macro variables

SAS creates read-only macro variables for you:

Name Holds
&SYSDATE9 Date the session started, such as 12NOV2026
&SYSTIME Start time
&SYSVER SAS version
&SYSUSERID Your user id
&SYSLAST Name of the last data set created
&SYSERR / &SYSCC Error status after a step
&SYSSCP Operating system family

Typical use:

SAS
title "Report created &sysdate9";
footnote "Last data set: &syslast";

Looking at values: %PUT

%put writes text to the log, which is how you debug macro code:

SAS
%put &=year &=region;        /* prints: YEAR=2025 REGION=East */
%put The year is &year..;
%put _user_;                 /* list all user-defined macro variables */
%put _automatic_;            /* list all automatic ones */

&=name prints both the name and the value, a modern shortcut worth knowing. Every macro debugging session starts with %put. Related system options are options symbolgen; (log every resolution), mprint (log macro-generated code) and mlogic (log macro logic).

Creating macro variables from data

%let can only store text you type. To store a value computed from a data set, use call symputx in a DATA step, or into in PROC SQL (see PROC SQL: Creating Tables and Macro Variables).

SAS
data _null_;
  set work.summary end=last;
  if last then call symputx("total_rows", _n_);
run;

%put &=total_rows;
  • call symputx(name, value) creates a macro variable at run time, and trims blanks and converts numbers automatically. (Its older cousin symput does not trim.)
  • data _null_; runs a DATA step without creating a data set.
  • Use symget("name") in a DATA step to read a macro variable's value at run time.

Timing is the key idea: %let and &var resolve before the step runs, while call symputx assigns during execution. So a macro variable created by call symputx cannot be used by the same step that created it, only by later steps.

Scope: global and local

Macro variables created at the top level of a program are global and stay until the session ends. Variables created inside a macro program (next lesson) are local to it by default. Control this explicitly:

SAS
%global batch_id;      /* declare before a macro creates it */
%local i total;        /* local to the current macro */
%symdel batch_id;      /* delete a global variable */

If a local and a global variable share a name, the local one wins inside the macro.

Stand-in: nested resolution

Python Stand-in: && and indirect references

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two ampersands collapse to one in the first pass, so &&year&n becomes &year2, and the second pass resolves that to 2025. This is how loops pick year1, year2, year3 in turn, which you will use in Conditional Logic and Loops in Macros.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does %let year = 2025; create?

    Macro variables are text; %let stores the characters 2025.
  2. 2. Which title shows the value of the macro variable region?

    Macro variables resolve inside double quotes but not inside single quotes.
  3. 3. With %let lib = sales; what does &lib..report resolve to?

    The first period ends the macro variable name and is consumed; the second remains as the library dot.
  4. 4. When does call symputx assign its macro variable?

    symputx works at run time, so the value is available to later steps, not to the same step's compilation.
  5. 5. Which statement lists all user-defined macro variables in the log?

    %put user writes the user-defined macro variables; automatic lists the system ones.

Summary

  • A macro variable stores text; create it with %let and use it as &name.
  • Resolve inside double quotes, never inside single quotes.
  • A period ends the name and is consumed; &lib..table needs two.
  • Automatic variables such as &sysdate9 and &syslast come free; debug with %put &=name.
  • call symputx creates macro variables from data at run time; mind global and local scope.

Next: Macro Programs and Parameters