Improving Program Efficiency with Macro Variables |
Creating Your Own Macro
Variables![]() ![]() |
Combining Macro Variable References with Prefixes
Now let's look at a more complex example of substituting text in programs. The following program is identical to the one you just worked on, except the name of the new data set has changed from Hrd.Newtemp to Hrd.Temp1999. title "Temporary Employees for 1999"; data hrd.temp1999; set hrd.temp; if year(enddate)=1999; run; proc print data=hrd.temp1999; run; Notice that the text you need to change each year again involves the value of the YEAR function. When creating a macro variable to substitute the YEAR value, you can also update the data set name, the subsetting IF statement, and the title text.
To begin, you use the %LET statement to create the macro variable
%let yr=1999; Then you reference this new macro variable in your program. Notice that the macro variable reference is combined with the prefix temp to create the data set name. The reference is placed immediately following the prefix. %let yr=1999; title "Temporary Employees for &yr"; data hrd.temp&yr; set hrd.temp; if year(enddate)=&yr; run; proc print data=hrd.temp&yr; run; When you combine macro variable references with prefixes, the SAS System forms the words for you. For example, you write the DATA statement in your program as follows: data hrd.temp&yr;
When the macro variable reference is resolved, the value of the macro variable
data hrd.temp1999; |
![]() ![]() ![]() ![]() ![]() ![]() |
|
![]() |
![]() |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.