Writing Macro Functions

From sasCommunity
Jump to: navigation, search

Q: How do I write a macro that works like a function?

This page contains some guidelines for writing macro functions.

  • a macro function is less than a statement, therefore do not generate a SAS semicolon.
  • use named parameters
  • Q: are you generating SAS statements or macro statements?

example.1:

%Macro DoThis(var=,value1=,value2)
/ des = 'description of macro'
  ;
&Var. = sum(&Value1.,&Value2)
%*Note: no semicolon on previous line!;
%Mend DoThis;
 
*usage:;
DATA Work.Sums;
%DoThis(var=X,value1=y,value2=z);
 
%DoThis(var=M,value1=N,value2=P);


example.2:

*name: DoThis.sas;
%Macro DoThis2(value1=,value2)
/ des = 'description of macro'
  ;
sum(&Value1.,&Value2)
%*Note: no semicolon on previous line!;
%Mend DoThis2;
 
*usage:;
DATA Work.Sums2;
 
X = %DoThis2(value1=y,value2=z);
 
if M or %DoThis2(value1=N,value2=P)
   then output;

References

-- created by User:Rjf2 15:06, 17 November 2009 (UTC)

--Ronald_J._Fehd macro.maven == the radical programmer