SAS Macro : Detailed Explanation of Macros

SAS Macro – In SAS, Macros were commonly used to automate repetitive task. In this Macro tutorial we will teach you how you can create macros in SAS to reduce your efforts towards repetitive tasks.

Lets take a simple example suppose you want to print multiple datasets if you are doing it without Macro you have to write proc print statement multiple times but we can reduce our effort by designing a SAS Macro to do it quickly.

We can create macro using %Macro <Macro Name> keyword and then ends it with %MEND <Macro Name> statement.

/*Macro Framework*/

%MACRO printTable(table);

PROC PRINT DATA=&TABLE;RUN;

%MEND;

/*Calling of Macro*/

%printTable(SASHELP.CARS);

 

In the above Macro example we have created a Macro named printTable(table) which takes table as an argument which is getting dynamically passed into Proc Print statement.

 

Once we have designed the Macro Framework now it’s time to reuse that code on multiple scenarios you don’t have to write the print statement multiple times you have to call the Macro with different dataset as a parameter.

/*Calling of Macro to print multiple dataset*/

%printTable(SASHELP.CARS);
%printTable(SASHELP.class);
%printTable(SASHELP.air);

Similarly you can use this real time scenario when you have to import or export multiple datasets in SAS. Just write the Macro framework and call it for multiple cases.

 

/*———————————————-*/
/* Types of Macro Variables in SAS */
/*———————————————-*/

In SAS we have two types of Macro Variables i.e Global Macro Variable and Local Macro Variable.

Global Macro Variables

All the Macro Variables which are declared or created in an open block statement are by default Global Variables.

/* User Defined Global Variables */

%let city=Dallas;
%let date=25APR2014;
%let amount=343;

 

Local Macro Variables

All the Macro Variables which are declared or created inside particular block or within block are by default Local Variables.To define Local variable we can use %local Keyword.

/* User Defined Local Variables */

%macro lvars;
%local name day;
%let name=Ed Norton;
%let day=Friday;
%put _local_; /* When we run this we will see the Local Variables in %lvar */
%mend;

 

/* When we run this we will see the Local Variables in %lvar */

%lvars;

/* This shows all global user and automatic macro variables */

%put _all_;

/*————————————————————–*/
/*SAS Macro %DO statement */
/*————————————————————–*/

The %DO statement designates the beginning of a section of a macro definition that is treated as a unit until a matching %END statement is encountered. This macro section is called a %DO group. %DO groups can be nested.Lets see an example with sample %DO Macro.

%macro shownote(length);
%if &length = LONG %then %do;
%put NOTE: This is a longer note.;
%put NOTE: It is shown if the macro parameter is set to the value LONG.;
%put NOTE: The default note is much shorter.;
%end;
%else %put NOTE: This is a short note;
%mend shownote;

%shownote(LONG);

 

Output of %DO statement

/* results should be:
  
 NOTE: This is a longer note.
 NOTE: It is shown if the macro parameter is set to the value LONG.
 NOTE: The default note is much shorter.
 
 */

 /*———————————————-*/
/* SAS MACRO %IF statement */
/*———————————————-*/

 

%macro testif;
%local x y z;
%let x=10;
%let y=5;
%let z=0;
%if &x > &y %then %put test 1 successful;
%else %put test 1 failed;
%if &x < &y %then %put test 2 failed;
%else %put test 2 successful;
%if &x %then %put test 3 successful;
%else %put test 3 failed;
%if &z %then %put test 4 failed;
%else %put test 4 successful;
%mend testif;

%testif

Output of %IF statement

/* results should be:
test 1 successful
test 2 successful
test 3 successful
test 4 successful
*/

/*———————————————-*/
/* Macro Parameters in SAS */
/*———————————————-*/

/* Positional Parameters */

%macro pospar(var1,var2,var3,var4,var5);
%put &var1 &var2 &var3 &var4 &var5;
%mend pospar;

%pospar();/*Macro Calling*/
/* results should be: *blank*  because we have not passed any argument while calling Macro*/
%pospar(str1,str2,str3,str4,str5);
/*  results should be: str1 str2 str3 str4 str5  */
%pospar(str1)
/* results should be: str1 because we have passed single parameter */

/* Keyword Parameters */

 

  %macro keypar(var1=str1,var2=str2,var3=);
  %put &var1 &var2 &var3;
  %mend keypar;

  %keypar();/*Macro Calling*/
/* results should be: str1 str2 because 2 parameter already having default values while Macro creation */
%keypar(var1=abc,var2=def,var3=ghi);
/* results should be: abc def ghi because all parameters have values */

/*———————————————-*/
/* Macro Quoting  in SAS*/
/*———————————————-*/

In SAS Macro Quoting is used to handle quotation or some special characters let’s see some real time examples.

%macro m;
%put this is macro m;
%mend m;

%let p=%str(proc print; run;);
%put p = &p;
/* results should be:  p = proc print; run; */
 %let m=%nrstr(look at macro %m and var &m);
  %put m = &m;
  /* results should be: m = look at macro %m and var &m */
 data test;
    store="Tom's place";
    call symput('s',store);
run;

%macro test;
   %if %bquote(&s) ne %then %put *** valid ***;
   %else %put *** null value ***;
%mend test;
%test;
/* results should be:  *** valid *** */
 %let bqtvar=%str(macro%'s here!);
     %put bqtvar = &bqtvar;
 /* results should be: bqtvar = macro's here! */
 %let nbqtvar=%nrstr(macro%'s and &procs);
 %put nbqtvar = &nbqtvar;
/*---------------------------
   results should be:
      nbqtvar = macro's and &procs
   ---------------------------*/
  %put superqed variable = %superq(nbqtvar);
  /* results should be:  superqed variable = macro's and &procs */
  %let qsc=%qscan(&nbqtvar,1);
  %put qsc = &qsc;
   /* results should be: qsc = macro's  */
  %let qsu=%qsubstr(&nbqtvar,1,7);
  %put qsu = &qsu;
  /* results should be: qsu = macro's */
  %let qsp=%qupcase(&nbqtvar);
  %put qsp = &qsp;
 /* results should be:  qsp = MACRO'S AND &PROCS */
  %let mvar=%nrstr(%m);
  %put mvar = %unquote(&mvar);
  /*---------------------------
    results should be:
this is macro m
mvar =
   ---------------------------*/

/*———————————————-*/
/* Macro Char Functions */
/*———————————————-*/

/*——————————*/
/* %EVAL FUNCTION */
/*——————————*/

%let num1=%eval(32767+7233);
%put &num1 should be 40000;

/*——————————*/
/* %INDEX FUNCTION */
/*——————————*/

%let a=a very long value;
%let b=%index(&a,v);
%put v appears at position &b..; /* at 3 */

/*——————————*/
/* %LENGTH FUNCTION */
/*——————————*/

%macro aproc;
proc print; run;
%mend aproc;

%put %length(&a) should be 17;

%put %length(%aproc) should be 16;

/*——————————*/
/* %SCAN FUNCTION */
/*——————————*/

%LET P=CATS AND DOGS;
%LET Q=%SCAN(&P,3,%str( ));
%put &q SHOULD BE DOGS;

/*——————————-*/
/* %substr function */
/*——————————-*/

%LET FIRST=THIS BOOK;
%LET SECOND=%SUBSTR(&FIRST,6,4);
%put &second SHOULD BE BOOK;

/*——————————-*/
/* %UPCASE function */
/*——————————-*/

%let string=%upcase(a string to upcase);
%put &string should be A STRING TO UPCASE;