I’m going to commit the cardinal sin and admit that my heart isn’t hugely into writing this. I’m plowing the SAS Advanced Study Guide, I’m at page 300, and I’m exactly a third of the way through it, and that is the easy part. And the truth, the hard truth, is that I should be plowing SSRS and SSAS over this. But, I’m in, and so we continue the process.
Macro variables allow you to dynamically write code in your programs. For instance, you can use them to define a WHERE clause in a PROC SQL statement or even define the name of a data set. SAS likes to demo them for writing footers and titles.
There are two kinds of macro variables: automatic and user-defined. Automatic are system defined and do things like return the date, time, SAS release or other system information.
In order to use a macro variable you define it with a %LET statement and then you use it by preceding it with a & symbol (this is actually more involved, but that’s in another article).
- They are character data, always.
- Because they are characters, expressions, too, are characters, and, as such, are not evaluated.
- Case is retained.
- Quotation marks are retained.
- Leading and trailing blanks are removed.
So, when you defined a macro variable, these are all perfectly acceptable.
LET %myvar = 123; /* 123 */ LET %myvar = abraham lincoln; /* abraham lincoln */ LET %myvar = Abraham Lincoln; /* Abraham Lincoln */ LET %myvar = Abraham's horse; /* Abraham's horse */ LET %myvar = Abraham + Horse * Cow; /* Abraham + Horse * Cow */ LET %myvar = 1 + 2 + 3 / 4; /* 1 + 2 + 3 / 4*/
You can also define a macro variable with a macro variable.
LET %var = "My Variable"; /* "My Variable" */ LET %var1 = &var; /* "My Variable" */
LET %var = var1; /* var1 */
LET &var = Billy Bob; /* var1 will equal Billy Bob */
So, you can not only assign a variable the contents in a variable, you can create a variable based on the value in a variable.
Here is a relatively simple example (that somehow vanished from WordPress) of macro variables.
%LET mysex = F; proc sql outobs=5; SELECT name, age, sex, weight, "&sysdate" FROM mysql.admit WHERE sex = '&mysex"; quit; Name Age Sex Weight ------------------------------------------------ Almers, C 34 F 152 20APR13 Bonaventure, T 31 F 123 20APR13 Johnson, R 43 F 137 20APR13 Reberson, P 32 F 151 20APR13 Eberhardt, S 49 F 172 20APR13
Here is what the code does. It creates a macro variable name mysex and sets its values to F. In the SELECT statement, the automatic variable &sysdate is used to generate the column with all the 20APR13 values. Then, finally, the WHERE clause is set to WHERE sex = F.
One thing to note, the examples in the book all show a single-quote, macro variable, double-quote in the code samples. Neither that, or single-quotes worked for me. I had to use double-quotes to get macro variables to work. I can’t guarantee that this is correct. It might be some quirk of where I’m writing this code that I don’t know about. Anyways, my examples will all use double-quotes.
I’m going to stop at this point. The second half of this second lists a number of options and they make a good post by themselves. Fortunately, most of them should be familiar from SAS BASE.
At page 315, only 591 more to go. Sheesh!
Oh, and the best part of this post, WordPress ate my sample code the first time I posted it. Yee haa!