SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Improving Program Efficiency with Macro Variables
Creating Your Own Macro Variables


Combining Macro Variable References with Suffixes

You can also combine macro variable references with suffixes. For example, the subsetting IF statement in this DATA step uses the YEAR function to extract the year value from the data set variable EndDate.

     %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;

Sometimes, however, you need to run this program using the data set variable BeginDate in the subsetting IF statement.

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

You can create a macro variable to easily change the beginning or ending date variable throughout the program. You create a macro variable named period to contain the first three digits of the variable name that you want to create, either end or beg, using the %LET statement.

Then you reference the macro variable in the subsetting IF statement and concatenate the suffix date to finish the variable name.

Alert: When combining a macro variable prefix and a suffix, you must separate them with a period. SAS software has no way of knowing where you macro variable reference ends and the suffix begins if you don't separate them with a period.

When the IF statement resolves, SAS software reads &period as the macro variable reference. Then it replaces the reference with the value of period to create the variable name EndDate.

The program looks like this:

     %let yr=1999;
     %let period=end;
     title "Temporary Employees for &yr";
     data hrd.temp&yr;
          set hrd.temp;
          if year(&period.date)=&yr;
     run;
     proc print data=hrd.temp&yr;
     run;

  back||next


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

Terms of Use & Legal Information | Privacy Statement