When a variable is not a variable

0

New SAS coders beware. “SAS variables” are variables in the statistics sense, not the computer programming sense. SAS has what many computer languages call “variables,” it just calls them “macro variables.” Knowing the difference between SAS variables and SAS macro variables will help you write more flexible and effective code.

What is a SAS Variable?

Since statistics are the heart of SAS, a SAS variable is defined like a variable you’d encounter in statistics.  In statistics, a variable is an attribute that can change across observations.  Statistics variables are often visualized as columns in tables.  For example, Name, Sex, Age, and Height are variables in the following excerpt from the CLASS table in SASHELP.

SAS variables

What a SAS Variable Is Not

In many computer languages, a variable saves a value, and then the variable can be used to reference the value.  For example, the following Python code assigns and uses the variable x:

We can use macro variables to accomplish this functionality in SAS code.  A macro variable saves a text value, then the macro variable can be used to reference the value.  The following SAS code assigns and uses the macro variable x.

“%let x = 4;” assigns the macro variable, and “(obs=&x)” uses it.  “(obs=&x)” is effectively the same as “(obs=4)”.

While SAS macro variables can serve the purpose of variables in other languages, that is not their only function.  For a more in depth look at macro variables, check out SAS documentation (Introduction to Macro Variables) or take our SAS Macro Language 1: Essentials course, which focuses on the components of the SAS macro facility and how to design, write and debug macro systems. The course also spends a good deal of time helping students understand how programs and macro code are processed in SAS.

Share

About Author

Allison Saito

Senior Associate Technical Training Consultant

As a technical training consultant, Allison expands the potential of SAS programmers. Her favorite teaching moments are when students realize, "This will make X so much easier!" and "O! That's what my coworker's code means!". Before joining SAS, she earned a master's of economics from Duke University and a bachelor's of math from NC State.

Related Posts

Comments are closed.

Back to Top