SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Improving Program Efficiency with Macro Variables
Creating Your Own Macro Variables


Now that you are familiar with the %LET statement, let's create the macro variable for your program about temporary employees. Remember, you need to create a macro variable to contain the year value.
     title 'Temporary Employees for 1999';
     data hrd.newtemp;
        set hrd.temp;
        if year(enddate)=1999;
     run;
     proc print data=hrd.newtemp;
     run;

Your first report is about temporary employees who were hired in 1999, so you write the %LET statement as follows:

     %let year=1999;

After defining a macro variable, you need to reference the macro variable in your program. Referencing a user-defined macro variable is the same as referencing an automatic macro variable. You specify the macro variable name preceded by an ampersand.

     &year

And here's your revised program. The %LET statement defines your macro variable and the macro variable references appear throughout the program statements.

     %let year=1999;
     title "Temporary Employees for &year";
     data hrd.newtemp;
        set hrd.temp;
        if year(enddate)=&year;
     run;
     proc print data=hrd.newtemp;
     run;

Notice the placement of the %LET statement. This statement must appear before the macro variable references. Typically, the %LET statement is placed at the beginning of the program.

After you submit the program, the macro variable references are resolved by replacing each reference with the macro variable value. Macro variable references are resolved prior to DATA step compilation.

For example, your subsetting IF statement . . .

     if year(enddate)=&year;

. . . is processed by SAS software as follows:

     if year(enddate)=1999;

After your program compiles successfully, the program statements are executed, your data set is created, and you see the following PROC PRINT output.


Temporary Employees for 1999

Obs Agency ID Name
1 Administrative Support, Inc. F274 CICHOCK, ELIZABETH MARIE
2 Administrative Support, Inc. F101 BENINCASA, HANNAH LEE
3 OD Consulting, Inc. F054 SHERE, BRIAN THOMAS
4 New Time Temps Agency F077 HODNOFF, RICHARD LEE
5 Administrative Support, Inc. P039 CHEVARLEY, ARLENE ELSIE
6 New Time Temps Agency P378 LAMUTU, JULIE HOPE
7 Temporary Services, Inc. F462 AMAKIAN, DOROTHY ELIZABETH 
8 Administrative Support, Inc. F015 LAMUTO, JULIE HOPE
9 New Time Temps Agency P524 ABRAMSON, ANDREA CARLSON


Now that your program contains a macro variable, it will be easy to update each year. You simply change the value of the macro variable year, and your program is ready to use.
     %let year=2000;
     title "Temporary Employees for &year";
     data hrd.newtemp;
        set hrd.temp;
        if year(enddate)=&year;
     run;
     proc print data=hrd.newtemp;
     run;

back||next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement