Automation is at the heart of SAS© programming. A classic scenario involves checking the contents of a table before running a process: "If my table contains data, run the standard report; otherwise, run an anomaly report."
Although the logic seems simple, implementing it using the macro language can run into two major obstacles: variable scope and the misleading behavior of certain automatic variables like SQLOBS.
The Problem: Why Doesn't My Code See the Error?
Imagine code structured into two macros:
%Check_Data: Checks the data and creates an&errorflag.%Run_Report: Reads&errorand decides which report to print.
A common mistake is defining the &error variable inside the first macro without precaution.
1. The Scope Trap
By default, a macro variable created inside a macro (via %LET) is local. It only exists for the duration of that macro's execution.
As soon as %Check_Data finishes, the &error variable is destroyed. When %Run_Report tries to read &error, it finds nothing, and the program crashes (error "Apparent symbolic reference not resolved").
The Solution: The variable must be declared as global at the beginning of the program or before its use: