Sentry Page Protection
SAS Macro [2-15]
Referencing Macro Variable in Title
Referencing macro variable in title or footnotes requires special attention.
You must use double quotation when the title (or footnotes) includes a reference to the macro variable.
Example
Copy and run the TRANSAC data set from the yellow box below.
You must use double quotation when the title (or footnotes) includes a reference to the macro variable.
Example
Copy and run the TRANSAC data set from the yellow box below.
The TRANSAC data set contains a list of transactions, agents and transaction amount:
- TranID: Transaction ID
- Agent: Tom, Mary or John
- Amount: Transaction Amount
The code below computes the summary statistics (i.e. n, mean, standard deviation) for the transaction amount by the agent John.
Example
Title "Transaction Statistics: John";
Footnote1 "The table shows the transaction statistics by John.";
proc means data=transac;
var amount;
where agent = "John";
run;
The program looks great. It computes the transaction statistics for John with the correct title and footnote.
Now, let's add the proper macro variable to the program so that it is reusable for the other two agents.
Example
%let agent = John;
Title "Transaction Statistics: &agent";
Footnote1 "The table shows the transaction statistics by &agent.";
proc means data=transac;
var amount;
where agent = "&agent";
run;
This computes the same statistics as before (John).
Now, let's use the program to compute the statistics for Mary:
Example
%let agent = Mary;
Title "Transaction Statistics: &agent";
Footnote1 "The table shows the transaction statistics by &agent.";
proc means data=transac;
var amount;
where agent = "&agent";
run;
By simply assigning a different value to the macro variable AGENT, the program now computes the statistics for Mary instead.
No Quotation Needed
Please note that when assigning the text value (e.g. John, Mary) to the macro variable, no quotation was used:
Example
%let agent = John;
%let agent = Mary;
It is then inserted into your program where the macro variable was referenced (&dur):
When the program is executed, the macro variable reference (&dur) will be replaced by the value assigned to the macro variable (i.e. 20).
This will be the same as executing the following program instead:
Substituting text with macro variables is fairly easy.
In the next few sessions, you will learn a few more examples of how macro variables can make your program more dynamic and reusable.
Exercise
Reuse the program above and print the list of subscribers whose length of membership is less than 30 days.
Reuse the program above and print the list of subscribers whose length of membership is less than 30 days.
Need some help?
Fill out my online form.