Producing Detail Reports with the PRINT Procedure |
Understanding the SAS Macro Facility |
Base SAS includes the macro facility as a tool to customize SAS and to reduce the amount of text you must enter to do common tasks. The macro facility enables you to assign a name to character strings or groups of SAS programming statements.
From that point on, you can work with the names rather than with the text itself. When you use a macro facility name in a SAS program, the macro facility generates SAS statements and commands as needed. The rest of SAS receives those statements and uses them in the same way it uses the ones you enter in the standard manner.
The macro facility enables you to create macro variables to substitute text in SAS programs. One of the major advantages of using macro variables is that it enables you to change the value of a variable in one place in your program and then have the change appear in multiple references throughout your program. You can substitute text by using automatic macro variables or by using your own macro variables, which you define and assign values to.
Using Automatic Macro Variables |
The SAS macro facility includes many automatic macro variables. Some of the values associated with the automatic macro variables depend on your operating environment. You can use automatic macro variables to provide the time, the day of the week, and the date based on your computer's internal clock as well as other processing information.
To include a second title on a report that displays the text string "Produced on" followed by today's date, add the following TITLE statement to your program:
title2 "Produced on &SYSDATE9";
Notice the syntax for this statement. First, the ampersand that precedes SYSDATE9 tells the SAS macro facility to replace the reference with its assigned value. In this case, the assigned value is the date the SAS session started and is expressed as ddmmmyyyy, where
dd is a two-digit date | |
mmm is the first three letters of the month name | |
yyyy is a four-digit year |
The following program, which includes a PROC SORT step and the TITLE statement, demonstrates how to use the SYSDATE9. automatic macro variable:
options linesize=80 pageno=1 nodate; proc sort data=qtr04; by SalesRep; run; proc print data=qtr04 noobs split='/' width=uniform; var SalesRep Month Units AmountSold; format Units comma7. AmountSold dollar14.2; sum Units AmountSold; title1 'TruBlend Coffee Maker Quarterly Sales Report'; title2 "Produced on &SYSDATE9"; label SalesRep = 'Sales/Rep.' Units = 'Units/Sold' AmountSold = 'Amount/Sold'; run;
The following output shows the report:
Using Automatic Macro Variables
TruBlend Coffee Maker Quarterly Sales Report 1 Produced on 30JAN2001 Sales Units Amount Rep. Month Sold Sold Garcia 10 250 $7,742.50 Garcia 10 365 $11,304.05 Garcia 11 198 $6,132.06 Garcia 11 120 $3,716.40 Garcia 12 1,000 $30,970.00 Hollingsworth 10 530 $16,414.10 Hollingsworth 10 265 $8,207.05 Hollingsworth 11 1,230 $38,093.10 Hollingsworth 11 150 $7,425.00 Hollingsworth 12 125 $6,187.50 Hollingsworth 12 175 $5,419.75 Jensen 10 975 $30,195.75 Jensen 10 55 $1,703.35 Jensen 11 453 $14,029.41 Jensen 11 70 $2,167.90 Jensen 12 876 $27,129.72 Jensen 12 1,254 $38,836.38 ======= ============== 8,091 $255,674.02
Using Your Own Macro Variables |
In addition to using automatic macro variables, you can use the %LET statement to define your own macro variables and refer to them with the ampersand prefix. Defining macro variables at the beginning of your program enables you to change other parts of the program easily. The following example shows how to define two macro variables, Quarter and Year, and how to refer to them in a TITLE statement.
To use two macro variables that produce flexible report titles, first define the macro variables. The following %LET statements define the two macro variables:
%let Quarter=Fourth; %let Year=2000;
The name of the first macro variable is Quarter and it is assigned the value Fourth. The name of the second macro variable is Year and it is assigned the value 2000.
Macro variable names such as these conform to the following rules for SAS names:
In these simple situations, do not assign values to macro variables that contain unmatched quotation marks or semicolons. If the values contain leading or trailing blanks, then SAS removes the blanks.To refer to the value of a macro variable, place an ampersand prefix in front of the name of the variable. The following TITLE statement contains references to the values of the macro variables Quarter and Year, which were previously defined in %LET statements:
title3 "&Quarter Quarter &Year Sales Totals";
The complete program, which includes the two %LET statements and the TITLE3 statement, follows:
options linesize=80 pageno=1 nodate; %let Quarter=Fourth;1 %let Year=2000;2 proc sort data=qtr04; by SalesRep; run; proc print data=qtr04 noobs split='/' width=uniform; var SalesRep Month Units AmountSold; format Units comma7. AmountSold dollar14.2; sum Units AmountSold; title1 'TruBlend Coffee Maker Quarterly Sales Report'; title2 "Produced on &SYSDATE9"; title3 "&Quarter Quarter &Year Sales Totals";3 label SalesRep = 'Sales/Rep.' Units = 'Units/Sold' AmountSold = 'Amount/Sold'; run;
The following list corresponds to the numbered items in the preceding program:
The following output shows the report:
Using Your Own Macro Variables
TruBlend Coffee Maker Quarterly Sales Report 1 Produced on 12JAN2001 Fourth Quarter 2000 Sales Totals Sales Units Amount Rep. Month Sold Sold Garcia 10 250 $7,742.50 Garcia 10 365 $11,304.05 Garcia 11 198 $6,132.06 Garcia 11 120 $3,716.40 Garcia 12 1,000 $30,970.00 Hollingsworth 10 530 $16,414.10 Hollingsworth 10 265 $8,207.05 Hollingsworth 11 1,230 $38,093.10 Hollingsworth 11 150 $7,425.00 Hollingsworth 12 125 $6,187.50 Hollingsworth 12 175 $5,419.75 Jensen 10 975 $30,195.75 Jensen 10 55 $1,703.35 Jensen 11 453 $14,029.41 Jensen 11 70 $2,167.90 Jensen 12 876 $27,129.72 Jensen 12 1,254 $38,836.38 ======= ============== 8,091 $255,674.02
Using macro variables can make your programs easy to modify. For example, if the previous program contained many references to Quarter and Year, then changes in only three places will produce an entirely different report:
Copyright © 2012 by SAS Institute Inc., Cary, NC, USA. All rights reserved.