Macro Functions

Manipulate text

Perform (simple) arithmetic operations

Execute SAS functions

Have similar syntax as corresponding DATA step character functions

Yield similar results

Manipulate macro variables and expressions

Represent macro triggers

Are executed by the macro processor

Do not require quotation marks around character constant arguments

Arguments to macro string manipulation functions can be any text and/or macro triggers: constant text

macro variable references

macro functions

macro calls

Constant text arguments do not require quotation marks.

Some Macro Functions

%UPCASE -- translates letters from lowercase to uppercase.

In [ ]:
%let lower=lowercaseword;
%let upper=%UPCASE(lowercaseword) ;
%let upper2=%upcase(&lower);
%put lower=&lower upper=&upper upper2=&upper2;

%SUBSTR -- extracts a substring from a character string.

In [ ]:
%let str1=thisisastringwithoutspaces;
%let sub1=%SUBSTR(thisisastringwithoutspaces,5,2);
%let sub2=%SUBSTR(&str1,8,6);
%put str1=&str1  sub1=&sub1  sub2=&sub2
In [ ]:
%put date9=&sysdate9;

%put year=%substr(&sysdate9,6);

%SCAN -- extracts a word from a character string.

In [ ]:
%let str1=this is a string with spaces;
%let word1=%scan(&str1,2);
%let word2=%scan(this is a string with spaces,4);
%put str1=&str1  word1=&word1  word2=&word2;
In [ ]:
data tmp;
  do i=1 to 4;
  x=i**2;
  output;
  end;
run;  
%put syslast=&syslast;
%put dsn=%scan(&syslast,2,.);

Example -- Parsing a variable list

In [ ]:
/* a macro variable containing the names of the variables of interest*/
%let dietvars=PROTEIN FAT TOTAL_CARBOHYDRATE Alcohol CALCIUM 
              PHOSPHORUS IRON SODIUM POTASSIUM SATURATED_FAT 
              OLEIC_ACID LINOLEIC_ACID CHOLESTEROL;
%put Variables: &dietvars;
In [ ]:
/*Find the number of variables*/
 %let numvars=%sysfunc(countw(&dietvars));
 %put Number of variables=  &numvars;
In [ ]:
/*Find the fifth variable*/
 %let var5=%scan(&dietvars,5);
 %put Fifth variable= &var5;
In [ ]:
/*find the last variable*/
%let lastvar=%scan(&dietvars,&numvars);
%put Last variable=&lastvar;

%INDEX -- searches a character string for specified text.

In [ ]:
%let str1=this is simple text;
%let index1=%INDEX(this is simple text,is);
%let index2=%INDEX(&str1,text);
%put str1=&str1  index1=&index1 index2=&index2;

%EVAL -- performs arithmetic and logical operations.

In [ ]:
%let sum=2+7;
%let sum1=%EVAL(&sum);
%let sum2=%EVAL(&sum1>7);
%let sum3=%EVAL(&sum<=7;);
%put sum=&sum  sum1=&sum1 sum2=&sum2 sum3=&sum3;
In [ ]:
%let x=%eval(2+2);
%put x=&x;
%let z=3;
%let x=%eval(&z+2);
%put x=&x;
%let x=%eval(&x+&z);
%put &x;
In [ ]:
%let thisyr=2007;
%let lastyr=%eval(&thisyr-1);
%put thisyr=&thisyr  lastyr=&lastyr;
proc means data=mac1.order_fact maxdec=2 min max mean;
   class order_type;
   var total_retail_price;
   where year(order_date) between &lastyr and &thisyr;
   title1 "Orders for &lastyr and &thisyr";
   title2 "(as of &sysdate9)";
run;

%SYSFUNC -- executes SAS functions.

In [ ]:
%let name=DAN MCGEE;
%put name=&name;
%let name1=%sysfunc(propcase(&name));
%put name1=&name1;
In [ ]:
%let varlist=alc bmi sbp diab chol chd;
%let numvars=%SYSFUNC(countw("&varlist"));
%put varlist=&varlist numvars=&numvars;
In [ ]:
title1 "%sysfunc(today(),weekdate.)";
title2 "%sysfunc(time(),timeAMPM8.)";
data orders;
   set orion.Orders;
   where year(Order_Date)=2007;
   Lag=Delivery_Date - Order_Date;
run;

proc means data=orders maxdec=2 min max mean;
   class Order_Type;
   var Lag;
run;
title;
In [ ]:
%let dietvars=PROTEIN   FAT TOTAL_CARBOHYDRATE Alcohol   CALCIUM PHOSPHORUS IRON SODIUM
 POTASSIUM SATURATED_FAT OLEIC_ACID      LINOLEIC_ACID CHOLESTEROL;
%put &dietvars;
%let varlist=%sysfunc(compbl(&dietvars));
%put &varlist;
%let varlist=%sysfunc(translate(&varlist,","," "));
%put &varlist;

Sometimes one wants special character and/or macro triggers included in some text

This requires "quoting" functions.

%STR -- function masks (removes the normal meaning of) these special tokens:

+ - * / , < > = ; ' "
LT EQ GT LE GE NE AND OR NOT blank

Create a macro variable with a single line of sas code: proc means data=orion.payroll min max;

In the following, the first semi-colon is interpreted as the end of the macro definition so is no included in the text.

In [ ]:
%let str1=proc means data=orion.payroll min max;;
%put str1=&str1;

Use the %str function

In [ ]:
%let str1=proc means data=orion.payroll min max%str(;);
%let str2=%str(proc means data=orion.payroll min max;);
%put str1=&str1;
%put str2=&str2;

%NRSTR -- quotes special characters, including macro triggers.

In [ ]:
%let statement=%str(title "S&P 500";);
%put &statement;
In [ ]:
%let statement=%nrstr(title "S&P 500";);
%put &statement;